# Thread: Fibonacci numbers in C

1. ## Fibonacci numbers in C

Hi, I'm still following the same tutorial (This is NOT homework) and there's this problem in which you have to print X amount of Fibonacci numbers (I chose the first 10 numbers), but the program should get the numbers by itself.

For those who don't know, Fibonacci was a mathematician who came up with a series to solve a problem. The series goes like this: 0,1,1,2,3,5,8,13,21 . Each number is the sum of the 2 numbers before that number (3+5=8 and 8+5=13).

Given that F(n)=F(n-2)+F(n-1) and F(0)=1 and F(1)=1 (The tutorial gives this info too..)

This is the code that I have...

Code:
#include <stdio.h>
int main()
{
int x,f;
for (x=1, f=0;x<=10;f=(x-2)+(x-1),x+=1){
printf("The first ten numbers of the Fibonacci series are %d\n", f);
}
}
I tried adding a variable that represents the last number (F(n-1)) but it didn't really work so I scratched that.
Thanks
Alastor

2. >For those who don't know, Fibonacci was a mathematician who came up with a series to solve a problem. The series goes like this:
>0,1,1,2,3,5,8,13,21 . Each number is the sum of the 2 numbers before that number (3+5=8 and 8+5=13).
I would be shocked and amazed if you found a programmer that had never heard of the Fibonacci series. Hell, you'd be hard pressed to find any remotely experienced programmer who doesn't know at least two ways of generating the sequence. I can think of five off the top of my head, and I'm far from the best around here.

>This is the code that I have...
Your code exhibits undefined behavior because you forgot to return a value at the end of main. The algorithm is trying to be too concise as well. Try using three variables aside from the loop counter. One holds the first number, one holds the second number, and one is used as a temporary to calculate the third number:
Code:
#include <stdio.h>

int main ( void )
{
int x = 1, y = 0, z;
int i;

for ( i = 0; i < 10; i++ ) {
printf ( "F(%d): %d\n", i, x );
z = x + y;
y = x, x = z;
}

return 0;
}
This is the traditional non-recursive solution, and it falls under the category of "Things I should know because they can be applied to more interesting problems".

3. And I think the fibonacci sequence starts with 1, 1, 2, 3; not 0, 1, 1, 2, 3.

4. And you say tomato, I say tomato. You define 1 as the smallest natural number, I define 0 as a natural number. Mathematicians agreeing on a set of definitions is the exception, not the rule.

5. Thanks!

This is the traditional non-recursive solution, and it falls under the category of "Things I should know because they can be applied to more interesting problems".
2 things:
1. Define intersting
2. I wouldn't be asking for the answer if I knew it already, therefore I did NOT know this as an unexperienced programmer.

ps It has to start with 0, because you have to add 1 to something! (And that's 0! lol)

6. You are correct dwks. the only disagreement i've seen is if the series should start at n=0 or n=1. but its always 1 1 2 3

7. >And I think the fibonacci sequence starts with 1, 1, 2, 3; not 0, 1, 1, 2, 3.
And I think I've seen about a baker's dozen advocates of both. But since 0 has no value, I see no problem with adding it to the series. No weight, no problem!

>1. Define intersting
Using the same technique in reverse, I can write an efficient top-down red black tree without all kinds of fugly special cases.

>2. therefore I did NOT know this as an unexperienced programmer.
My point was that you don't need to explain things that you don't understand to us when asking a question about them. If we don't know what you're talking about (a rare occurance), we'll ask you to clarify.

8. google let me to here, which starts the sequence at 1.

9. s It has to start with 0, because you have to add 1 to something! (And that's 0! lol)
Um no it doesn't. Its called a defination. You define the first two values to be 1

10. Originally Posted by Prelude
My point was that you don't need to explain things that you don't understand to us when asking a question about them. If we don't know what you're talking about (a rare occurance), we'll ask you to clarify.
I see, thanks

edit: Thantos, I bet you a Wikipedia article

11. I'll bet you an algorithm book, a calculas book, and about 5 other books that aren't editted by anyone, everyone, and their mother

Edit: Besides all it shows is the orginal debate I mentioned about if it starts at n=0 or n=1. That article took the n=1 approach

12. I think it is going closer to "Egg first or Chicken first"

13. The fibonacci sequence is defined with the first two terms as 1, and each succeeding term the sum of the previous two.

At least, that's what I read.

Are there any articles that use the T1 = 0 approach?

14. Originally Posted by dwks
The fibonacci sequence is defined ...
Get it into your heads that there is no "is defined" in the mathematical literature. There is no single reference that gives definitions of everything. All that you can do is make it clear in context exactly what you mean when using certain terminology, to avoid ambiguity.

If you want to argue about the definition of the Fibonacci sequence, you could start by giving a coherent definition of 'sequence'.

15. Using your logic then the following is the fibonacci sequence
Code:
f(n) = {
π if n = 0
e if n = 1
f(n-1) + f(n-2) if n > 1
}
of course the majority of the world would reject it as the fibonacci sequence.