This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Fibonacci_Search | |
| { | |
| static int kk = -1, nn = -1; | |
| static int fib[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, | |
| 377, 610, 98, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, | |
| 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, | |
| 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, | |
| 102334155, 165580141 }; | |
| static int fibsearch(int a[], int n, long x) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function fib(n){ | |
| let arr = [0, 1] | |
| for (let i = 2; i < n + 1; i++){ | |
| arr.push(arr[i - 2] + arr[i -1]) | |
| } | |
| return arr[n] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Fibonacci(0) = 0, | |
| Fibonacci(1) = 1, | |
| Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include | |
| #include | |
| #include "search.h" | |
| typedef struct FibonacciValues | |
| { | |
| // Fibonacci number: F(n) = F(n-1) + F(n-2) | |
| int Fn_2; // F(n-2) | |
| int Fn_1; // F(n-1) | |
| int Fn; // F(n) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| bool isFibonacci(int w) | |
| { | |
| double X1 = 5 * Math.Pow(w, 2) + 4; | |
| double X2 = 5 * Math.Pow(w, 2) - 4; | |
| long X1_sqrt = (long)Math.Sqrt(X1); | |
| long X2_sqrt = (long)Math.Sqrt(X2); | |
| return (X1_sqrt*X1_sqrt == X1) || (X2_sqrt*X2_sqrt == X2) ; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function fibs(n){ | |
| let [a, b] = [0, 1] | |
| while (n > 0){ | |
| [a, b] = [b, a + b] | |
| n -= 1 | |
| } | |
| return a | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def F(n): | |
| if n == 0: return 0 | |
| elif n == 1: return 1 | |
| else: return F(n-1)+F(n-2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def fib_iterative(n): | |
| a, b = 0, 1 | |
| while n > 0: | |
| a, b = b, a + b | |
| n -= 1 | |
| return a |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function fib(n){ | |
| let arr = [0, 1]; | |
| for (let i = 2; i < n + 1; i++){ | |
| arr.push(arr[i - 2] + arr[i -1]) | |
| } | |
| return arr[n] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static double inverseSqrt5 = 1 / Math.Sqrt(5); | |
| static double phi = (1 + Math.Sqrt(5)) / 2; | |
| /* should use | |
| const double inverseSqrt5 = 0.44721359549995793928183473374626 | |
| const double phi = 1.6180339887498948482045868343656 | |
| */ | |
| static int Fibonacci(int n) { | |
| return (int)Math.Floor(Math.Pow(phi, n) * inverseSqrt5 + 0.5); | |
| } |