Skip to content

Instantly share code, notes, and snippets.

@JonyBepary
Last active March 10, 2020 20:58
Show Gist options
  • Save JonyBepary/3c4dddfdbc492a6f59ecbe839b0b25f2 to your computer and use it in GitHub Desktop.
Save JonyBepary/3c4dddfdbc492a6f59ecbe839b0b25f2 to your computer and use it in GitHub Desktop.
binet formula for fibonacci number check
// using binet formula for fibonacci number check
//author jony bepary
#include <stdio.h>
#include <math.h>
int gen_fibo(int n){
// using binet formula to generate fibonacci number.
// can use as fibnonacci checker dependency. or generate fibonacci series
double sqrt_5 = pow(5, .5); // sqrt(5) can be used
double F_1_minus = ((1-sqrt_5)/2);
double F_1_plus = ((1+sqrt_5)/2);
F_1_minus = pow(F_1_minus, n);
F_1_plus = pow(F_1_plus, n);
double res = floor((F_1_plus - F_1_minus)/sqrt_5);
return res;
}
check_fibo(int Fn){
double sqrt_5 = pow(5, .5);
// double F_1_minus = ((1-sqrt_5)/2);
double phi = ((1+sqrt_5)/2);
double res = (log10(Fn)+log10(sqrt_5))/log10(phi); // generating nth term of fibonnacci series.
printf("%d\n", Fn);
if ((int) gen_fibo(floor(res)) == Fn || (int) gen_fibo(ceil(res)) == Fn || Fn == 0)
// when input/Fn = 0 then res = inf thats why "|| Fn == 0" is included
{
printf("It's Fibonacci number\n");
return 1;
}else{
printf("It's not Fibonacci number\n");
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment