Code: Show/Hide unsigned int FibonacciRec(unsigned int N) { if (N==0) return 0; else if (N==1) return 1; else return FibonacciRec(N - 1) + FibonacciRec(N - 2); } |
Code: Show/Hide unsigned int FRO(unsigned int N, unsigned int Previous, unsigned int Current) { if (N <= 1) { return Current; } else { return FRO(N ? 1, Current, Current + Previous); } } unsigned int FibonacciRecOptimized(unsigned int N) { return (N == 0) ? (N) : (FRO(N, 0, 1)); } unsigned int FibonacciIter(unsigned int N) { unsigned int R1 = 0, R2 = 1, R = 0, i; for (i = 1; i <= N; i++) { R = R1 + R2; R2 = R1; R1 = R; } return R; } |
Code: Show/Hide printf("num bytes = %i\n",sizeof(long long int)); |
Samapico wrote: | |
|
Code: Show/Hide long long fib(int a, long long *mem) { return (a <= 1) ? (mem[a] = a) : ((mem[a] == -1) ? (mem[a] = fib(a-1,mem) + fib(a-2,mem)) : mem[a]); } long long getfib(int x) { int i; long long *memo = malloc(sizeof(long long)*x); for (i = 0; i < x; i++) memo[i] = -1; return fib(-1+x,memo); } |
Doc Flabby wrote: |
Alternativly write it in haskell ![]() |
Code: Show/Hide prod (a,b,c) (a',b',c') = (a*a'+b*b', a*b'+b*c', b*b'+c*c')
pow acc m 0 = acc pow acc m n = pow acc' (prod m m) (div n 2) where acc' | even n = acc | otherwise = prod m acc fib n = let (_,x,_) = pow (1,0,1) (1,1,0) n in x |
Bak wrote: |
hey sam what have you been up to these days? |
SamHughes wrote: |
[..]
Working in Boston, working on a relationship search engine. You? |
SamHughes wrote: |
You? |
YonatanNaamad wrote: |
It might not be directly applicable to a search engine, but it's somewhat relevant regardless. |
Bak wrote: |
[..]
Grad school. |