Fibonacci Sequence error with "1"

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 723 Views - Last Post: 11 June 2012 - 01:19 AM Rate Topic: -----

#1 Yeronox  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 99
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2091
  • View blog
  • Posts: 3,186
  • 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.
Was This Post Helpful? 1
  • +
  • -

#3 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • Posts: 4,163
  • 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

Was This Post Helpful? 1
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2091
  • View blog
  • Posts: 3,186
  • 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?
Was This Post Helpful? 0
  • +
  • -

#5 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • Posts: 4,163
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 Yeronox  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 99
  • 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

Was This Post Helpful? 0
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2091
  • View blog
  • Posts: 3,186
  • Joined: 21-June 11

Re: Fibonacci Sequence error with "1"

Posted 01 June 2012 - 09:06 AM

View Postaresh, 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.

View PostYeronox, 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.
Was This Post Helpful? 1
  • +
  • -

#8 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • Posts: 4,163
  • 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

Was This Post Helpful? 1
  • +
  • -

#9 Yeronox  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 99
  • Joined: 12-March 12

Re: Fibonacci Sequence error with "1"

Posted 01 June 2012 - 11:25 AM

View Postaresh, 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

Was This Post Helpful? 0
  • +
  • -

#10 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • Posts: 4,163
  • 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 :D j/k

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

Was This Post Helpful? 0
  • +
  • -

#11 Yeronox  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 99
  • 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.
Was This Post Helpful? 0
  • +
  • -

#12 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • Posts: 4,163
  • 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 :P 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

Was This Post Helpful? 0
  • +
  • -

#13 Yeronox  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 99
  • 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!
Was This Post Helpful? 0
  • +
  • -

#14 Mobuis1995  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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;
   long answer;
   
   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

Was This Post Helpful? 0
  • +
  • -

#15 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2091
  • View blog
  • Posts: 3,186
  • Joined: 21-June 11

Re: Fibonacci Sequence error with "1"

Posted 01 June 2012 - 02:44 PM

View PostMobuis1995, 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.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2