By definition first 2 Fibonacci numbers are defined as 0 and 1. nth Fibonacci number can be computed as sum of (n-2)th & (n-1)th Fibonacci numbers
Hence the Fibonacci numbers are as follows:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
A number is given to you, how will you check if that number is a Fibonacci number or not ?
History of Fibonacci Numbers (reference, wiki..):
Indians can feel proud that The Fibonacci sequence first appeared in Indian mathematics, in connection with Sanskrit prosody.
In the Sanskrit oral tradition, there was much emphasis on how long (L) syllables mix with the short (S), and counting the different patterns of L and S within a given fixed length results in the Fibonacci numbers; the number of patterns that are m short syllables long is the Fibonacci number Fm + 1.
Susantha Goonatilake writes that the development of the Fibonacci sequence “is attributed in part to Pingala (200 BC), later being associated with Virahanka (c. 700 AD), Gopala (c.1135 AD), and Hemachandra (c.1150)“. Parmanand Singh cites Pingala’s cryptic formula misrau cha (“the two are mixed“) and cites scholars who interpret it in context as saying that the cases for m beats (Fm+1) is obtained by adding a [S] to Fm cases and [L] to the Fm-1 cases. He dates Pingala before 450 BCE.
However, the clearest exposition of the series arises in the work of Virahanka (c. 700AD), whose own work is lost, but is available in a quotation by Gopala.
In the West, the Fibonacci sequence first appears in the book Liber Abaci (1202) by Leonardo of Pisa, also known as Fibonacci. And as it is with most of the other stuff, the numbers are named after Fibonacci and called Fibonacci numbers.
Computing Fibonacci Number:
First 2 Fibonacci numbers are fixed as 0 & 1. As given in the Question, You can compute the nth Fibonacci number using (n-1)th & (n-2)th fibonacci numbers:
Fn = Fn-1 + Fn-2
These numbers also comes in shallow diagonal of Pascal triangle: see this picture.
Finding if a number is Fibonacci number or not:
One way to check if a number is Fibonacci is to keep finding Fibonacci numbers till we get a number greater than or equal to. For example, to check if a number ‘n’ is Fibonacci or not:
int checkfibonacci(int n) { int a = 0; int b = 1; if (n==a || n==b) return true; int c = a+b; while(c<=n) { if(c == n) return true; a = b; b = c; c = a + b; } return false; }
Another method (Quick one) to check if a number if Fibonacci number or not, is as below:
N is a Fibonacci number if and only if ( 5*N2 + 4 ) or ( 5*N2 – 4 ) is a perfect square!
For Example:
16 Comments
smoothly explained.
thanks for your grat explination
formula for check number is Fibonacci if number is sum of last 3 numbers
This does not work, I’ve tested it multiple times with different values and it all came back false. Im very disappointed.
Maybe you are calculating 5*n^2+4 in the code, but in c,c++ it should be 5*n*n-4.
you are stupid! you can use function pow (n,2) in include
No u!
Change the condition for the while loop
Replace < by <=
It will give correct output.
////try this
int main()
{
int w;
printf(“enter a number you want to check “);
scanf(“%d”, &w);
int a[1000];
a[0]=0;
a[1]=1;
int i=1,j;
while(a[i]<=w){
i=i+1;
a[i]=a[i-1]+a[i-2];
}
for(j=0;j<=i;j++){
printf("%d ", a[j]);
}
if(a[j-2]==w){
printf(" it is fibonacci number");
}else{
printf(" not fibonacci");
}
}
works if the condition in 8th line is changed to while (c<=n)
This will 100% work. And it’s compact as well…
#include
#include
#include
int main()
{
int n,calc1,calc2,pwr;
printf(“Enter a number : “);
scanf(“%d”,&n);
pwr= pow(n,2)+0.5;
calc1=(5*pwr)+4;
calc2=(5*pwr)-4;
if(sqrt(calc1)==round(sqrt(calc1)) || sqrt(calc2)==round(sqrt(calc2)))
{
printf(“The number is Fibonacci”);
}
else
{
printf(“The number is not Fibonacci”);
}
getch();
return(0);
}
What are the names of your files
how can I do this with recursion
int Fibonacci(int a)
{
if (a==0) return 0;
else if(a==1)
return 1;
else
return ( Fibonacci(a-1)+ Fibonacci(a-2));
}
visit: https://studyfame.com/c-program/fibonacci-series-program-in-c-using-recursion
smoothly explained.
yes