int getFib(int n){ if(n==0) return 0; else if(n==1) return 1; else return (getFib(n-1) + getFib(n-2)); }//end of getFib
int value() { if(_value <= 2) return 1; Fib f1 = getFib(_value - 1); Fib f2 = getFib(_value - 2); return f1.value() + f2.value(); }
int getFib(int num) { int first_fib = 0; int second_fib = 1; if (num == 1) return first_fib; else if (num == 2) return second_fib; else return getFib(num - 1) + getFib(num - 2); }
int main(void) { for (int i = 0; i <= 44; ++i) fibs[i] = -1; int n; scanf("%d", &n); printf("%d\n", getFib(n, 0)); return 0; }
// Used for fib/factor since it's different from add,sub etc. // due to it requiring only one parameter // @fundex == choice of math function based our functions[] table // @para is the string of a number being passed in char * doMoreMath(int fundex, char * para){ char* a = malloc(strlen(para)+1); a = strcpy(a,para); int fpara = atoi(a); int * ret = NULL; char * str_ret = NULL; if(fundex == 5){ printf("we're computing the prime factors for %d\n", fpara); int * factors = getFactors(fpara); ret = factors; int i; char * str_temp = (char *)malloc(sizeof(char)*1000); printf("sizeof is %d\n",(int)(sizeof(factors)/sizeof(factors[0])+1)); for(i = 0; factors[i] != -1; i++){ printf("ret[%d] is %d\n",i,ret[i]); if(str_ret==NULL){ str_ret = malloc(sizeof(char)*1000); sprintf(str_ret,"%d",ret[i]); } else{ sprintf(str_temp, "%d", ret[i]); str_ret = strcat(str_ret, str_temp); } str_ret = strcat(str_ret, "\n"); }//end of for str_ret = strcat(str_ret, "\0"); }//end of prime factors else if(fundex == 7){ printf("we're getting the first %d fibonacci numbers\n",fpara); int i; int * fibs = (int *)malloc(sizeof(int)*fpara); for(i = 0; i < fpara; i++){ fibs[i] = getFib(i); }//end of for ret = fibs; char * str_temp = malloc(sizeof(char)*1000); for(i = 0; i < fpara; i++){ if(str_ret==NULL){ str_ret = malloc(sizeof(char)*1000); sprintf(str_ret,"%d",ret[i]); } else{ //str_temp = (char *)malloc(sizeof(char)*1000); printf("ret[%d] is %d\n",i,ret[i]); sprintf(str_temp,"%d",ret[i]); str_ret = strcat(str_ret,str_temp); } str_ret = strcat(str_ret, "\n"); }//end of for str_ret = strcat(str_ret, "\0"); }//end of fibonacci return str_ret; }//end of domoremath
int main() { prints("Fibonacci Number using Matrix Multiplication O(logn)\n"); int i; for(i = 0; i < 45; i++) { printi(i); prints(".\t"); printi(getFib(i)); prints("\n"); } return 0; }
pf_result * fib_1_svc(int *argp, struct svc_req *rqstp) { static pf_result result; result.num = getFib(*argp); result.time = 0; return &result; }
int getFib(int termNum, int depth) // returns fibonacci number; { static int numCall = 0; ++numCall; for (int i = 0; i < depth; ++i) { #ifndef NDEBUG printf(" "); #endif } #ifndef NDEBUG printf("#%d getFib(%d, %d)\n", numCall, termNum, depth); #endif if (fibs[termNum] != -1) return fibs[termNum]; if (termNum == 0) return 1; if (termNum == 1) return 1; fibs[termNum] = getFib(termNum - 1, depth + 1) + getFib(termNum - 2, depth + 1); return fibs[termNum]; }