I'm relatively new to processing so apologies if I've missed something obvious in the following. After combing over the forums I've yet to find a post referencing Binet's -
I'm looking to calculate the Nth Fibonacci number using the following code based on Binet's formula:
Thanks!
I'm looking to calculate the Nth Fibonacci number using the following code based on Binet's formula:
- int FibN(int FibNum) {
- float phi = (1+sqrt(5))/2;
- float res = (pow(phi,FibNum)-pow((-1/phi),FibNum)) / sqrt(5);
- return int(res);
- }
- FibN(30) = 832040 // Correct
- FibN(31) = 1346269 // Correct
- FibN(32) = 2178309 // Correct
- FibN(33) = 3524579 // Incorrect - should be 3524578
- FibN(34) = 5702888 // Incorrect - should be 5702887
- FibN(46) = 1836312704 // Should be 1836311903
Thanks!
1