Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
can any one help me by finding a way
how to right a program to check whether the number belongs to fibonacci series
without counting the whole fibonacci numbers?????


fibonacci series math code is
f(x)=f(x-1)+f(x-2) x>=2

the counting code is easy but i need to check a number entered by user!!!!
Posted

As suggested by Resmi the mathemaical approach is the best for your question.

However, if memory is not a constaint and you want your application for repetitive use.
I suggest go for a simplified approach of Dynamic programming

1)Keep a Dynamic Array []
2)Whenver input is given do a binary search in Array
3) if found , jump to step 5
4)If not found compute the series till you get the Fibonacci number starting from N-1 and Nth element in the Array and add the result to Array
5) send the response to user

This will make your program much faster and performance will increase with the input list.


Best Regards,
Raina
 
Share this answer
 
That's described in wikipedia:
http://en.wikipedia.org/wiki/Fibonacci_number[^], chapter "Recognizing Fibonacci numbers".
 
Share this answer
 
hi,
ask the user to input a number, store that in a variable.In a loop,
start generating fibonacci number one by one using your formula and compare each number with users input, if this is same as users input,then display mesage accordingly.
if its NOT same,then generate next number of the series and go on till you get a match.
this is a basic logic flow.I hope you can improve on this and code yourself.

happy coding !!
 
Share this answer
 
Comments
yamen-nassif 29-Mar-12 10:26am    
a quite nice idea i didn't catch

thanks a lot
suppose you input the value to the variable N

Then following code checks whther N belongs to Fib series

C++
double dRes1 = sqrt((5*pow(N, 2))-4);
int nRes1 = (int)dRes1;
double dDecPoint1 = dRes1 - nRes1;

double dRes2 = sqrt((5*pow(N, 2))+4);
int nRes2 = (int)dRes2;
double dDecPoint2 = dRes2 - nRes2;

if( !dDecPoint1 || !dDecPoint2 )
{
    cout<< N << " is a Fib series member";
}
else
{
    cout<< N << " Not Fib series member";
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900