Fibonacci number

I wrote the following program to check whether a number lies in the series of fibonacci number(s),

A number "x" is said to be in the series if it satisfies one or both of the following
conditions :
1) 5x^2 - 4 is a perfect square
2) 5x^2 +4 is a perfect square


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 int main()
{
    unsigned int x;
    long double p1,p2;
    long long int p3,p4;
    
    cin>>x;
    p1 =sqrt(5 * (pow(x, 2)) + 4);
    p2 =sqrt(5* (pow(x, 2)) - 4);
    p3 =sqrt(5* (pow(x, 2)) + 4);
    p4 =sqrt(5* (pow(x, 2)) - 4);
    
    if(  p3 == p1 || p4 == p2) {
        cout<<"isFibo";
    
    }
    else{
        cout<<"isNotFibo";
    }
    cout<<"\n";
   
}



but I am failing in one or many of the following test cases :

3960298668
5024537591
7836005813
8012912888
6765709805
2528408671
7900376214
2282303326
8887869919
3563535898
6780581879
21
6606294343
4852322041
9292527736
7914155152
6584405107
6013163775
1251663594
8097388134
5507376230
2931490299
9197518132
5322239976
5702887
8546740149
3204617640
8491571622
5427578315
4801496300
6396259706
1809565946
5452521974
3199301231
1409127554
4269279443
1125690559
8476689250
2166331449
313992755
558719035
4189071408
6693560599
7654354699
658943614
4852322041
390881871
1164300318
4660962706
6474198126
1857518387
1105642541
7690676253
4440171046
7559697825
239397742
4642457023
4765781447
5534742167
82708113
9235635609
4320686337
3179943394
44349448
8374361834
6013163775
5982009152
196418
7617265577
3566851720
5792018910
6079154763

What changes should i make in the code to satisfy the above test cases …
Thanks !!
Last edited on
1
2
3
4
    p1 =sqrt(5 * (pow(x, 2)) + 4);
    p2 =sqrt(5* (pow(x, 2)) - 4);
    p3 =sqrt(5* (pow(x, 2)) + 4);
    p4 =sqrt(5* (pow(x, 2)) - 4);


um. look at the above, and try to imagine a single case in which P3 will not = p1, or where p4 will not = p2.
tons of cases but when the output will be same it will not differential between a long long int and a long double ….try simple numbers like 7 or 8 or any number which is like within 1-100000…...
tons of cases? how about never. p1 and p3 are the exact same calculations, as are p4 and p2.
they are exact same calculations but the stored value type is different ….

sample input giving correct answers :

3028455434
9148500612
3292692109
3822190089
7866085129
8012912888
2667995244
7010162887
9205020319
4813968787
939418871
6013163775
7281005807
8514855781
6369128304
5768788913
6558985503
1753533532
7836005813
ok, my bad. it's late :)
vxk, the big number of you test case, may out range, long long int for 5*x^2
x=3028455434;
5*x^2+4 = 45857711578620641784 // 20 digits --> long long int can't hold!
what can i do so that i can hold it ….do i have to make separate function ..or there is some hacky way to do this …
thanks!
here you can get results more than thousand digits:
https://defuse.ca/big-number-calculator.htm
In the list you posted in your first post, only three are fibonacci numbers anyway:

21
196418
5702887

So your list is mixed between fib and non-fib. Is your program reporting incorrectly for all the values you listed?

this expands the range a little:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void testfib (long double fibtry)
{
long long int ptesttint, mtestint;
long double ptestdbl,mtestdbl;

ptestdbl = sqrt(5 * pow(fibtry,2) + 4);
mtestdbl = sqrt(5 * pow(fibtry,2) - 4);

 // convert to long long int

ptestint = ptestdbl;
mtestint = mtestdbl;

if ( ptestdbl - ptestint > 0 && mtestdbl - mtestint > 0)
   // not fib
else
  // fib
}


this got me to element 72 with accurate results; after 72, the test wasn't accurate, because fibtry, fibtry + 1, fibtry -1 all were reported as fib numbers.
Last edited on
notice that,
long long int x = 45857711578620641784; //not ok
long long int x = 3028455434; // ok

so, you can use normal Fibonacci loop/recursion. stop it when your current Fibonacci number is >= x.

if loop stops at == , then its a Fibonacci number
if at, >, its not.
Last edited on
Topic archived. No new replies allowed.