#include #include #include #include #include #include "bignums.h" #if 0 #define NUM1_SIZE 15 #define NUM2_SIZE 15 static inline char randchar() { return (rand() % 9) + '0'; } #endif static char * fibonacci(unsigned long long term) { char *prev1, *prev2, *res; prev2 = malloc(2); assert(prev2); strcpy(prev2, "0"); if(term < 3) { if(term == 1 || term == 2) strcpy(prev2, "1"); } return prev2; } term--; prev1 = malloc(2); assert(prev1); strcpy(prev1, "1"); while(term--) { res = add(prev1, strlen(prev1), prev2, strlen(prev2)); free(prev2); prev2 = prev1; prev1 = res; } free(prev2); return res; } int main(int argc, char *argv[]) { char *result; #if 0 char num1[NUM1_SIZE+1], num2[NUM2_SIZE+1], *result; int i; srand(time(NULL)); for(i = 0; i < NUM1_SIZE; i++) num1[i] = randchar(); num1[NUM1_SIZE] = '\0'; for(i = 0; i < NUM2_SIZE; i++) num2[i] = randchar(); num2[NUM2_SIZE] = '\0'; printf("\t%s\n", num1); printf("+\t%s\n", num2); printf("=\n"); result = add(num1, NUM1_SIZE, num2, NUM2_SIZE); printf("\t%s\n", result); free(result); #endif if(argc >= 2) { result = fibonacci(atoll(argv[1])); printf("%s\n", result); free(result); } else { printf("%s \n", argv[0]); } return 0; }