๐ฆ๐บ
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 int fibonacci(int n) { | |
| if (n < 2) return n; | |
| return 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
| 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
| def F(): | |
| a,b = 0,1 | |
| while True: | |
| yield a | |
| a, b = b, a + b |
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
| 3/2 = 1.5 | |
| 5/3 = 1.666666666... | |
| ... | |
| 233/377 = 1.618055556... |
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) { | |
| if (n < 2){ | |
| return n | |
| } | |
| return fib(n - 1) + fib (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
| from math import * | |
| phi = 1.61803399 | |
| sqrt5 = sqrt(5) | |
| def F(n): | |
| return int((phi**n - (1-phi)**n) /sqrt5) | |
| def isFibonacci(z): | |
| return F(int(floor(log(sqrt5*z,phi)+0.5))) == z |
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
| public static void BinaryTreeToDLL(Node root) { | |
| if (root == null) | |
| return; | |
| BinaryTreeToDLL(root.left); | |
| if (prev == null) { // first node in list | |
| head = root; | |
| } else { | |
| prev.right = root; | |
| root.left = prev; | |
| } |
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){ | |
| if (n === 1) return 0; | |
| if (n === 2) return 1; | |
| return fib(n โ 1) + fib(n โ 2); | |
| } |