# Fibonacci Sequence error with "1"

• (2 Pages)
• 1
• 2

## 16 Replies - 1548 Views - Last Post: 11 June 2012 - 01:19 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=281438&amp;s=bd02916a944e14e401e0ac318d55565b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Yeronox

Reputation: 2
• Posts: 102
• Joined: 12-March 12

# Fibonacci Sequence error with "1"

Posted 01 June 2012 - 07:08 AM

```// Fibonacci Sequence – Enter a number and have the program generate the Fibonacci sequence to that number or to the Nth number.
#include <iostream>
#include <cstring>
int main()
{
using namespace std;
const int kNum = 1000000;
cout << "Enter a number: ";
int number;
cin >> number;
cout << "The next number following the Fibonacci Sequence is: ";
long double result = (1.619047619047 * number)+0.49;
cout << static_cast<int> (result) << endl;
int a; cin>>a;
return 0;
}

```

This seems to work, but try it entering " 1 " ...
How to avoid it? (without using loops)

Is This A Good Question/Topic? 0

## Replies To: Fibonacci Sequence error with "1"

### #2 sepp2k

• D.I.C Lover

Reputation: 2620
• Posts: 4,175
• Joined: 21-June 11

## Re: Fibonacci Sequence error with "1"

Posted 01 June 2012 - 08:12 AM

```long double result = (1.619047619047 * number)+0.49;
```

How did you arrive at the formula? That doesn't look right to me.

You also probably should use rounding instead of casting the number (which truncates the number instead of rounding it) to get more accurate result. That doesn't really help as long as the formula is wrong though.

### #3 aresh

• It's a 16-Bit World!

Reputation: 273
• Posts: 4,258
• Joined: 08-January 12

## Re: Fibonacci Sequence error with "1"

Posted 01 June 2012 - 08:42 AM

I also thought the same and so left the topic, but when I tried entering a number, it magically produced correct results for EVERY number in the series....
EDIT : What result do you want when you enter 1 ?? The series is 1,1,2.... so 2 is also a right answer. But if you want 1, try using if.

This post has been edited by aresh: 01 June 2012 - 08:43 AM

### #4 sepp2k

• D.I.C Lover

Reputation: 2620
• Posts: 4,175
• Joined: 21-June 11

## Re: Fibonacci Sequence error with "1"

Posted 01 June 2012 - 08:48 AM

Oh, you're right it does work. I misunderstood what the formula was supposed to do. So then I guess I don't understand what the problem is.

When I enter 1 as input, I get 2 as output. Since 2 is the number that comes after 1 in the sequence (after the second 1 anyway) that is the expected output, no?

### #5 aresh

• It's a 16-Bit World!

Reputation: 273
• Posts: 4,258
• Joined: 08-January 12

## Re: Fibonacci Sequence error with "1"

Posted 01 June 2012 - 08:54 AM

Exactly what I said :-) But still, can you tell me how you got that formula ?? And sepp, if if you will put values, you will see that truncation is the right thing to do. eg, if you enter 1, 1.61+0.49=2.1 or something... so that part he has done right. If he wants 1 as the output after entering 1, he should use if statement.

### #6 Yeronox

Reputation: 2
• Posts: 102
• Joined: 12-March 12

## Re: Fibonacci Sequence error with "1"

Posted 01 June 2012 - 08:57 AM

These are the first ones I took:
```1 1 2 3 5 8 13 21 34
```

34/x=55
x=1.619047619047
I thought that with 1.619047619047*number I could have a number near the number I was searching, and I saw I needed to round it but I don't know how to do it for now (as I'm following a guide).
The formula came out when I thought about what the user had to enter: a user must enter a number, and I must find something that, with * or /, gives me the next number.
So I tried with a couple numbers and I had that result.

----
Yea, the problem is that the first number is 1 and it should gives the output "1". If there's no chance of gettin' it without using "if", I'll wait till I learn how it works

This post has been edited by Yeronox: 01 June 2012 - 09:02 AM

### #7 sepp2k

• D.I.C Lover

Reputation: 2620
• Posts: 4,175
• Joined: 21-June 11

## Re: Fibonacci Sequence error with "1"

Posted 01 June 2012 - 09:06 AM

aresh, on 01 June 2012 - 05:54 PM, said:

And sepp, if if you will put values, you will see that truncation is the right thing to do.

Yes, I just realized that the whole point of him adding 0.49 to the result before truncation is that he is doing rounding.

So basically I've consistently missed the point in my entire first post in this thread.

PS: The reason that he can get the next fibonacci number by multiplying the current one by 1.619047619047, is that that's the golden ratio.

Yeronox, on 01 June 2012 - 05:57 PM, said:

Yea, the problem is that the first number is 1 and it should gives the output "1". If there's no chance of gettin' it without using "if", I'll wait till I learn how it works

But why do you want the output to be 1 if the user enters 1? If your end goal is to generate the Fibonacci sequence by repeatedly multiplying with Phi that won't work either way. If nextFib(1) returns one, repeatedly applying nextFib would just give you the sequence 1,1,1,1.

If your only goal is to print the number that comes after a given a number in the sequence then letting nextFib(1) be 2 isn't any less correct than it being 1.

### #8 aresh

• It's a 16-Bit World!

Reputation: 273
• Posts: 4,258
• Joined: 08-January 12

## Re: Fibonacci Sequence error with "1"

Posted 01 June 2012 - 09:10 AM

BTW your result is wrong !!! I tested it !! I tested it from the following code :
```#include<iostream>
using namespace std;
int main()
{
long a=1,b=1,c=2,c1=2;
for(int i=0;i<100;i++)
{
c=a+b;
c1=(1.619047619047*B)/>+0.49;
cout << c << " " << c1 << endl ;
if(c!=c1)
{
cout << "WRONG RESULT";
break;
}
a=b;
b=c;
}
getchar();
return 0;
}

```

If you run the following, you will see that 14th term is wrong by 1 in your case (it gives 978 instead of 977).
EDIT : Please don't start on my bad coding practices, this is just a dirty program programmed to test Yeronox's result.

This post has been edited by aresh: 01 June 2012 - 10:01 AM

### #9 Yeronox

Reputation: 2
• Posts: 102
• Joined: 12-March 12

## Re: Fibonacci Sequence error with "1"

Posted 01 June 2012 - 11:25 AM

aresh, on 01 June 2012 - 04:10 PM, said:

BTW your result is wrong !!! I tested it !! I tested it from the following code :
```#include<iostream>
using namespace std;
int main()
{
long a=1,b=1,c=2,c1=2;
for(int i=0;i<100;i++)
{
c=a+b;
c1=(1.619047619047*B)/>+0.49;
cout << c << " " << c1 << endl ;
if(c!=c1)
{
cout << "WRONG RESULT";
break;
}
a=b;
b=c;
}
getchar();
return 0;
}

```

If you run the following, you will see that 14th term is wrong by 1 in your case (it gives 978 instead of 977).
EDIT : Please don't start on my bad coding practices, this is just a dirty program programmed to test Yeronox's result.

You're right: by entering "610" it gives an error.
Anyway it is due to my not exact result: by using numbers like 63245986 and 102334155 to get the number to use to multiply instead of 33 and 55 I'd have gotten another result, more accurate.
You can replace the "long double result" line with this:
```long double result = (1.6180339887498948 * number)+0.4;
```

And you won't get any error at the 14th number, but later you'll see an error

This post has been edited by Yeronox: 01 June 2012 - 11:27 AM

### #10 aresh

• It's a 16-Bit World!

Reputation: 273
• Posts: 4,258
• Joined: 08-January 12

## Re: Fibonacci Sequence error with "1"

Posted 01 June 2012 - 11:26 AM

What do you mean ??
EDIT : And yeah, I know I am great j/k

This post has been edited by aresh: 01 June 2012 - 11:27 AM

### #11 Yeronox

Reputation: 2
• Posts: 102
• Joined: 12-March 12

## Re: Fibonacci Sequence error with "1"

Posted 01 June 2012 - 11:33 AM

I mean that if you use this code:
```// Fibonacci Sequence – Enter a number and have the program generate the Fibonacci sequence to that number or to the Nth number.
#include <iostream>
#include <cstring>
int main()
{
using namespace std;
const int kNum = 1000000;
cout << "Enter a number: ";
int number;
cin >> number;
cout << "The next number following the Fibonacci Sequence is: ";
long double result = (1.6180339887498948 * number)+0.4;
cout << static_cast<int> (result) << endl;
int a; cin>>a;
return 0;
}

```

You'll be able to have right numbers at least until 165580141, but maybe later you'll find some error.

### #12 aresh

• It's a 16-Bit World!

Reputation: 273
• Posts: 4,258
• Joined: 08-January 12

## Re: Fibonacci Sequence error with "1"

Posted 01 June 2012 - 11:44 AM

The 79th term is wrong in this case, being 14472334024676222 instead of 14472334024676221 Now, what do you want to do ?? What result do you want when you input 1 ??

This post has been edited by aresh: 01 June 2012 - 11:44 AM

### #13 Yeronox

Reputation: 2
• Posts: 102
• Joined: 12-March 12

## Re: Fibonacci Sequence error with "1"

Posted 01 June 2012 - 02:14 PM

I don't see any other good formula to use so I'll leave this code for now; thanks for all replies anyway!

### #14 Mobuis1995

Reputation: 1
• Posts: 42
• Joined: 15-February 12

## Re: Fibonacci Sequence error with "1"

Posted 01 June 2012 - 02:38 PM

This formula may be worth a look:

```int main()
{
int number;

cout << "Enter a number: " << endl;
cin >> number;

double x = sqrt(5)/5;
double termOne = x * pow((1+sqrt(5))/2, number);
double termTwo = -x * pow((1-sqrt(5))/2, number);
answer = (long) (termOne + termTwo);

cout << "The next number following the Fibonacci Sequence is: " << answer << endl;
system("pause");
return 0;
}
```

This post has been edited by Mobuis1995: 01 June 2012 - 02:39 PM

### #15 sepp2k

• D.I.C Lover

Reputation: 2620
• Posts: 4,175
• Joined: 21-June 11

## Re: Fibonacci Sequence error with "1"

Posted 01 June 2012 - 02:44 PM

Mobuis1995, on 01 June 2012 - 11:38 PM, said:

This formula may be worth a look:

You're calculating something else though. Your code says for example that the number that comes after 4 is 3. Obviously that's not true. 3 is the 4th fibonacci number, but that's not what the OP's code calculates.