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
| // Iterative Solution for Fibonacci Numbers | |
| function *fibonacci(n) { | |
| const infinite = !n && n !== 0; | |
| let current = 0; | |
| let next = 1; | |
| while (infinite || n--) { | |
| yield current; | |
| [current, next] = [next, current + next]; | |
| } |
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(): | |
| a,b = 0,1 | |
| while True: | |
| yield a | |
| a, b = b, a + b | |
| def SubFib(startNumber, endNumber): | |
| for cur in F(): | |
| if cur > endNumber: return | |
| if cur >= startNumber: |
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
| private static final int FIB_0 = 0; | |
| private static final int FIB_1 = 1; | |
| private int calcFibonacci(final int target) { | |
| if (target == 0) { return FIB_0; } | |
| if (target == 1) { return FIB_1; } | |
| return calcFibonacci(target, 1, FIB_1, FIB_0); | |
| } |
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 int fibMemo[]; | |
| public static int fibByRecMemo(int num) { | |
| if (num == 0) { | |
| fibMemo[0] = 0; | |
| return 0; | |
| } | |
| if (num == 1 || num == 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
| function fib(n, a = 0, b = 1){ | |
| if (n > 0) { | |
| return fib(n - 1, b, a + b) | |
| } | |
| 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
| class Solution { | |
| int fib(int N) { | |
| if (N <= 1) { | |
| return N; | |
| } | |
| int[][] A = new int[][]{{1, 1}, {1, 0}}; | |
| matrixPower(A, N-1); | |
| return A[0][0]; | |
| } |
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
| const sqrt = Math.sqrt; | |
| const pow = Math.pow; | |
| const fibCalc = n => Math.round( | |
| (1 / sqrt(5)) * | |
| ( | |
| pow(((1 + sqrt(5)) / 2), n) - | |
| pow(((1 - sqrt(5)) / 2), 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
| def SubFib(startNumber, endNumber): | |
| n = 0 | |
| cur = f(n) | |
| while cur <= endNumber: | |
| if startNumber <= cur: | |
| print cur | |
| n += 1 | |
| cur = 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
| private static long fibonacci(int n) { | |
| double pha = pow(1 + sqrt(5), n); | |
| double phb = pow(1 - sqrt(5), n); | |
| double div = pow(2, n) * sqrt(5); | |
| return (long)((pha - phb) / div); | |
| } |