# Fibonacci, my arch enemy

• 03-10-2005
StarOrbs
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.
• 03-10-2005
sean
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)`
• 03-10-2005
StarOrbs
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
• 03-10-2005
Zach L.
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.
• 03-10-2005
Krak
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.
• 03-10-2005
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..
• 03-10-2005
Krak
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. :cool:
• 03-10-2005
Zach L.
```#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;     } }```