Fibonacci, my arch enemy

This is a discussion on Fibonacci, my arch enemy within the C++ Programming forums, part of the General Programming Boards category; Okay this program I understand fibonacci and how it works and if a sequence of numbers is or isn't. Now ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    59

    Fibonacci, my arch enemy

    Okay this program I understand fibonacci and how it works and if a sequence of numbers is or isn't.

    Now I have to implement this into c++. The program will ask how long the sequence of numbers are and will determine if that sequence of numbers is fibonacci or not.

    I can't figure out when to input the numbers and when to transfer previous numbers to a temp variable. I could easily do this if it didn't have a user inputted number of times. I've always had trouble doing this kind of thing perhaps after this one I will finally understand how to do it. Basically I've spent many hours trying different combinations and I figured I would finally ask for some help. Thanks in advanced.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Use the user-inputted number as a bound on your loop, and then just go through the loop checking the truthfulness of statements like
    Code:
    if(array[i+2] == array[i+1] + [array]+2)

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    If it were only that easy I'm only allowed to use what I have "learned" in class and we haven't got to arrays yet. We've done loops and ifs and everything else simpler than than that. somehow I have to do if's to determine if the previous number plus another equals the 3rd one or something like that. Ah, I have a headache from this one. lol

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Well, you only need to store the number the user just entered, and the previous two.

    Pseudo-Code:
    Code:
    X1 = User Input
      If X1 != 1 Then Not Fib
    X2 = User Input
      If X2 != 1 Then Not Fib
    
    While User Still Wants To Test (The User-Entered Sequence Length)
      X3 = User Input
      If X3 != X1 + X2 Then Not Fib
      Else
       X1 = X2
       X2 = X3
    You might also want to account for the fact that the user may start with 0 (as in 0 1 1 2 3 ...) so the first input might be 0 or 1, but the second must be 1.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Well sir, you're going to be using yourself a lot of for loops.

    Here's how you'd establish your numbers:
    Code:
    unsigned long Fibonacci[100];
    unsigned long addOn=0;
    
     for(int i=0; i < 100; i++){
         Fibonacci[i] = 1+addOn;    //Start with 1, plus our current add-on
         Sequence += Fibonacci[i-1];    /*Our add on will always be
         the number BEFORE the one we're working with.*/
    
         cout << "Number "<<i<<": "<<Fibonacci[i]<<endl;
    }
    Then just write a for loop to sift through the array and see if any values == any of the elements of the array. If not, it's not a Fibonacci number.
    Last edited by Krak; 03-10-2005 at 11:21 PM.

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    if you haven't gotten to arrays yet, i'm doubting you've gotten to recursion yet. but if you have, look at solving it that way. the logic of recusion for fibonacci isn't that tough, and the code is shorter.. but if you haven't gotten to recursion yet, don't worry about it..

  7. #7
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Quote Originally Posted by alpha
    if you haven't gotten to arrays yet, i'm doubting you've gotten to recursion yet. but if you have, look at solving it that way. the logic of recusion for fibonacci isn't that tough, and the code is shorter.. but if you haven't gotten to recursion yet, don't worry about it..
    The for-loop/array method's best.

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    To quote the thread's author:
    I'm only allowed to use what I have "learned" in class and we haven't got to arrays yet.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  9. #9
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Here's a cryptic solution:
    Code:
    #include <iostream>
    #include <cmath>
    int main()
    {
        using namespace std;
        int num;
        cout << "How many numbers?";
        cin >> num;
        double p1 = 1 + sqrt(5.0);
        double p2 = 1 - sqrt(5.0);
        for (int n=1; n<=num; ++n)
        {
            int i;
            cout << "Input number " << n << ":";
            cin >> i;
            double Fibn = (pow(p1,n) - pow(p2,n)) / ( pow(2.0,n)* sqrt(5.0) );
            if (i != int(Fibn + 0.5) )
                cout << "Not a fibonnacci sequence!" << endl;
        }
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  10. #10
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    Thanks Zach I was able to get my program to work with something similar to that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fibonacci prime
    By dbzx in forum C Programming
    Replies: 5
    Last Post: 04-17-2009, 12:13 PM
  2. Recursive Fibonacci, some help needed
    By cwafavre in forum C Programming
    Replies: 8
    Last Post: 11-04-2007, 02:20 PM
  3. array of pointer objects
    By Death_Wraith in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 08:06 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. The enemy, freind, and Inocents.
    By gamegod3001 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 10-08-2001, 09:55 PM

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