Skip to content

Instantly share code, notes, and snippets.

@jordonbiondo
Created January 16, 2013 08:20
Show Gist options
  • Save jordonbiondo/4545485 to your computer and use it in GitHub Desktop.
Save jordonbiondo/4545485 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#define aSize 1001
// 8 bit data type
typedef char byte;
int thousandDigitFib();
int addTo(byte* , byte*, int, int);
//print debugger
//void printNum(byte*, int);
int main(void) {
printf("%d\n", thousandDigitFib());
}
int thousandDigitFib() {
byte a[aSize] = {0};
byte b[aSize]= {0};
byte c[aSize]= {0};
a[aSize - 1] = 0;
b[aSize-1] = 1;
c[aSize-1] = 1;
int digits = 1;
int count = 1;
while(1) {
count += 1;
memcpy(c, b, aSize);
int digits = addTo(&b, &a, aSize, digits+1);
memcpy(a, c, aSize);
if (digits >= 1000) return count;
}
return count;
}
int addTo(byte* a, byte* b, int size, int end) {
int carry = 0;
int temp = 0;
int digits = 0;
for (int i = (size - 1); i >= size-end; i--) {
temp = a[i] + b[i] + carry;
if (temp) digits = (size-i);
carry = (temp >=10);
a[i] = (temp % 10);
}
return digits;
}
/*
void printNum(byte* a, int size) {
byte printZ = 0;
for (int i = 0; i < size; i++) {
if(!printZ) {
if(a[i]){
printZ = 1;
printf("%d",a[i]);
}
} else {
printf("%d",a[i]);
}
}
puts("");
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment