Create fibonacci_formula.c (#961)

This commit is contained in:
GrandSir 2022-09-30 11:51:43 +03:00 committed by GitHub
parent 7e6276b730
commit 6121ab5c20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 1 deletions

View File

@ -149,7 +149,7 @@
* [Spirograph](https://github.com/TheAlgorithms/C/blob/master/graphics/spirograph.c) * [Spirograph](https://github.com/TheAlgorithms/C/blob/master/graphics/spirograph.c)
## Greedy Approach ## Greedy Approach
* [Djikstra](https://github.com/TheAlgorithms/C/blob/master/greedy_approach/djikstra.c) * [Dijkstra](https://github.com/TheAlgorithms/C/blob/master/greedy_approach/dijkstra.c)
* [Prim](https://github.com/TheAlgorithms/C/blob/master/greedy_approach/prim.c) * [Prim](https://github.com/TheAlgorithms/C/blob/master/greedy_approach/prim.c)
## Hash ## Hash
@ -267,6 +267,7 @@
* [Fibonacci](https://github.com/TheAlgorithms/C/blob/master/misc/fibonacci.c) * [Fibonacci](https://github.com/TheAlgorithms/C/blob/master/misc/fibonacci.c)
* [Fibonacci Dp](https://github.com/TheAlgorithms/C/blob/master/misc/fibonacci_dp.c) * [Fibonacci Dp](https://github.com/TheAlgorithms/C/blob/master/misc/fibonacci_dp.c)
* [Fibonacci Fast](https://github.com/TheAlgorithms/C/blob/master/misc/fibonacci_fast.c) * [Fibonacci Fast](https://github.com/TheAlgorithms/C/blob/master/misc/fibonacci_fast.c)
* [Fibonacci Formula](https://github.com/TheAlgorithms/C/blob/master/misc/fibonacci_formula.c)
* [Gcd](https://github.com/TheAlgorithms/C/blob/master/misc/gcd.c) * [Gcd](https://github.com/TheAlgorithms/C/blob/master/misc/gcd.c)
* [Is Armstrong](https://github.com/TheAlgorithms/C/blob/master/misc/is_armstrong.c) * [Is Armstrong](https://github.com/TheAlgorithms/C/blob/master/misc/is_armstrong.c)
* [Large Factorials](https://github.com/TheAlgorithms/C/blob/master/misc/large_factorials.c) * [Large Factorials](https://github.com/TheAlgorithms/C/blob/master/misc/large_factorials.c)

59
misc/fibonacci_formula.c Normal file
View File

@ -0,0 +1,59 @@
/**
* @file
* @brief Finding Fibonacci number of any `n` number using [Binet's closed form formula](https://en.wikipedia.org/wiki/Fibonacci_number#Binet's_formula)
* compute \f$f_{nth}\f$ Fibonacci number using the binet's formula:
* Fn = 15 * (1+5 / 2)^n+1 15 * (15 / 2)^n+1
* @author [GrandSir](https://github.com/GrandSir/)
*/
#include <math.h> /// for pow and sqrt
#include <stdio.h> /// for printf
#include <assert.h> /// for assert
/**
* @param n index of number in Fibonacci sequence
* @returns nth value of fibonacci sequence for all n >= 0
*/
int fib(unsigned int n) {
float seq = (1 / sqrt(5) * pow(((1 + sqrt(5)) / 2), n + 1)) - (1 / sqrt(5) * pow(((1 - sqrt(5)) / 2), n + 1));
// removing unnecessary fractional part by implicitly converting float to int
return seq;
}
/**
* @brief Self-test implementations
* @returns void
*/
static void test () {
/* this ensures that the first 10 number of fibonacci sequence
* (1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89)
* matches with algorithm
*/
assert(fib(0) == 1);
assert(fib(1) == 1);
assert(fib(2) == 2);
assert(fib(3) == 3);
assert(fib(4) == 5);
assert(fib(5) == 8);
assert(fib(6) == 13);
assert(fib(7) == 21);
assert(fib(8) == 34);
assert(fib(9) == 55);
assert(fib(10) == 89);
printf("All tests have successfully passed!\n");
}
/**
* @brief Main function
* @returns 0 on exit
*/
int main() {
test(); // run self-test implementations
for(int i = 0; i <= 10; i++){
printf("%d. fibonacci number is: %d\n", i, fib(i));
}
return 0;
}