Last edited by QuantumPete; 10-16-2008 at 10:40 AM.
"No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
"Have you tried turning it off and on again?" - The IT Crowd
This is what I get:
Obviously it's wrapped around, since i'm only using ints instead of unsigned long longs. I've also got a version that uses a run-time populated table to speed up calculation.Code:time ./fib.tsk 50 Result = -298632863 real 13m27.65s user 11m42.11s sys 0m4.31s
QuantumPeteCode:time ./fib_table.tsk 50 Fib for 50 = -298632863 real 0m0.02s user 0m0.01s sys 0m0.01s
"No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
"Have you tried turning it off and on again?" - The IT Crowd
That's "useless" too, since you might as well use the formulaOriginally Posted by MK27
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Except the original poster is complaining that the result is inexact - I suspect it's simply that the double precision isn't good enough...
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
"No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
"Have you tried turning it off and on again?" - The IT Crowd
This is similar to what matsp posted, except it will take arbitrary numbers:
It even prints out all the numbers as it computes themCode:typedef unsigned long long ull; ull fib_t (int num, ull * precalc) { if (num <= 2) { return 1; } if (precalc[num] == 0) { precalc[num] = fib_t(num-1, precalc) + fib_t (num-2, precalc); printf ("%d = %llu\n", num, precalc[num]); } return precalc[num]; } int main (int argc, char ** argv) { int num = 0; ull * table = 0; if (argc != 2) { printf ("You must specifiy a number\n"); return 1; } num = atoi (argv[1]); table = calloc (num+1, sizeof (ull)); if (table == 0) { printf ("malloc failed!\n"); return 1; } printf ("Fibonacci Number for %d = %llu\n", num, fib_t(num, table)); free (table); return 0; }
QuantumPete
"No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
"Have you tried turning it off and on again?" - The IT Crowd
Checking the Wikipedia entry on the Fibonacci numbers, I see that one can actually derive it directly from the recurrence relation presented there. I also recall that it was the introductory example of at least one algorithms textbook. Consequently, I suggest that you work through such a text to help you understand recursion better.Originally Posted by MK27
Yes, I think so too.Originally Posted by matsp
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
If a formula that is not difficult to implement produces the result as fast and as accurately as using memoization, I would certainly prefer the formula since it has constant space complexity.Originally Posted by QuantumPete
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
"No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
"Have you tried turning it off and on again?" - The IT Crowd
Mathematically, the formula is indeed another way to compute the nth number in the original Fibonacci series, but in practice, it is an approximation since we do not have infinite precision floating point arithmetic. On the other hand, the approximation can be accurate, since we are dealing with integers in the end.Originally Posted by QuantumPete
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Okay, I'm really busted by this one! Short of reading the recommended book, can anyone help explain it? I used the following
./a.out 3Code:#include <stdio.h> int fib (int num) { int x, y; printf("fib: %d\n", num); if (num <= 2) { return 1; } x=fib(num-1); y=fib(num-2); printf("x: %d y: %d\n", num); fflush(stdout); return x+y; } int main (int argc, char *argv[]) { int answer,i; for (i=1;i<=atoi(argv[1]);i++) { printf("\nto fib: %d\n", i); answer=fib(i); printf("answer: %d\n", answer); } }
to fib: 1
fib: 1
answer: 1
to fib: 2
fib: 2
answer: 1
to fib: 3
fib: 3
fib: 2
fib: 1
x: 3 y: 0
answer: 2
How does x end up as 3, y as 0, and the answer as 2?
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Because your printf isn't being given the right data:
If it's gcc, -Wall will tell you that you have one too few arguments, even if it is not telling you that num isn't x or y.Code:printf("x: %d y: %d\n", num);
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Yeah that was dumb. I'm staring at the real output now.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
I still don't get it....[later] maybe I do...
to fib: 5 okay
fib: 5 right...
fib: 4
fib: 3
fib: 2
fib: 1
x: 1 y: 1 that makes sense
fib: 2 and this
x: 2 y: 1 so finally x's x is 2 (1+1)
fib: 3 y's x (?)
fib: 2
fib: 1
x: 1 y: 1
x: 3 y: 2 x is 2+1, y is 1+1
answer: 5
Is that right? I'm shaky about the last bit.
Last edited by MK27; 10-16-2008 at 09:49 AM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
I am looking at some equations for calculating arbitrary fibonacci numbers and I am not seeing anything terribly like the one which you are using. Your equation itself is wrong. That is why your output is wrong. And as its been said several times over, iteratively calculating these numbers is not a terrible chore and it need only be done once, tabled, and used on-demand.