Fibonacci numbers in C

This is a discussion on Fibonacci numbers in C within the C Programming forums, part of the General Programming Boards category; Hi, I'm still following the same tutorial (This is NOT homework) and there's this problem in which you have to ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    53

    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. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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".
    My best code is written with the delete key.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    And I think the fibonacci sequence starts with 1, 1, 2, 3; not 0, 1, 1, 2, 3.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    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. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    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)
    Last edited by Alastor; 10-08-2005 at 03:52 PM. Reason: Forgot to write something

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.
    My best code is written with the delete key.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    google let me to here, which starts the sequence at 1.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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. #10
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    Quote 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
    Last edited by Alastor; 10-08-2005 at 04:06 PM. Reason: I had to add something

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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. #12
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    I think it is going closer to "Egg first or Chicken first"
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    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?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote 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. #15
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fibonacci numbers (using class)
    By -chr- in forum C++ Programming
    Replies: 3
    Last Post: 01-21-2009, 03:49 PM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 10:15 AM
  4. FIBONACCI NUMBERS, please help!
    By JamesAnthony23 in forum C Programming
    Replies: 5
    Last Post: 09-26-2002, 03:39 PM
  5. fibonacci numbers?????
    By help!! in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2001, 11:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21