Thread: FIBONACCI NUMBERS, please help!

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    2

    FIBONACCI NUMBERS, please help!

    My instructions were to write a program that displays the first 20 elements of the Fibonacci series, that begins with 0, 1, then the next numbers come from adding the preceeding two numbers.
    0, 1, 1, 2, 3, 5, 8...

    Here is what I have so far:

    #include <stdio.h>

    int main(void)
    {
    int current, prev = 1, prevprev = 0;
    for (int i = 0; i < 20 i++)
    {current = prev + prevprev;
    printf(current + " ");
    prevprev = prev;
    prev = current;
    }
    println();
    }
    }


    Do I have the right idea? Please help...
    Also part of my instructions (i'm not sure if how to do this part either) are that I only display 5 members of the series per line, then start a new line, and they must be dspaced neatly in 5 columns across the screen, and display some appropriate title...boy these instructors are neat freaks!

    thanks for you time and help,
    -James

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    137
    This will calculate your numbers. You'll have to do the screen formatting yourself
    Code:
    #include <stdio.h>
    
    int main ()
    {
    	int bob [2];
       printf("%d\n",0);
       bob [0] = 1;
       bob [1] = 2;
       int mem;
       printf("%d\n%d\n",bob[0],bob[1]);
       for (int i=0;i<=16;i++) {
       	mem = bob[1];
       	bob [1] += bob[0];
          printf("%d\n",bob[1]);
          bob [0] = mem;
       }
    }
    http://uk.geocities.com/ca_chorltonkids

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    The Fibonacci series is defined as:

    Fib (0) = 0
    Fib (1) = 1
    Fib (n) = Fib (n-1) + Fib (n-2)

    This could be implemented like this:

    Code:
    int fib [N];
    int i;
    
    /* Print first N Fibonacci numbers */
    for (i = 0; i < N; i++)
    {
        if (i == 0)
        {
            fib [i] = 0;
        }
        else if (i == 1)
        {
            fib [i] = 1;
        }
        else
        {
            fib [i] = fib [i - 1] + fib [i - 2];
        }
    }
    
    /* Print them nice */
    for (i = 0; i < N; i++)
    {
        if (i % 5 == 0)
        {
            printf ("\n");
        }
    
        printf ("%d\t", i, fib [i]);
    }

  4. #4
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    It could be implemented as a recursive function also. But for larger numbers this would not be feasible. Shiro's method demonstrates the algorithm but takes up memory if you print out too many elements.

    You and crag have the right idea. You can accomplish printing and calculating fib values using only 2 variables. Perhaps illustrating this will give you an idea how to implement this:
    Code:
    var1  var2
    ----  ----
    0       1         <---- initial values
    1       2
    3       5
    8       13
    I think you can see the pattern now.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  5. #5
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Another way is to do this
    phi = 1/2(1 + sqrt(5))
    fib(n) = round((phi^n)/sqrt(5))
    You can try this on your calculator.

  6. #6
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Yup. The Golden Ratio!
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

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, 04: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. Fibonacci numbers in C
    By Alastor in forum C Programming
    Replies: 23
    Last Post: 10-10-2005, 01:45 PM
  4. 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, 11:15 AM
  5. fibonacci numbers?????
    By help!! in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2001, 11:07 PM