1
+ // ---------------- Fibonacci Sequence ----------------
2
+ // 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144...
3
+
4
+ // Time Complexity: O(2^n) Space Complexity: O(2^n)
5
+ function fibonacciRecursive ( nthElement : number ) : number | any {
6
+ // Base Cases
7
+ if ( nthElement === 1 ) return 0 ;
8
+ if ( nthElement === 2 ) return 1 ;
9
+ //Input Validation
10
+ if ( nthElement < 1 ) return undefined ;
11
+
12
+ // Recursive Call: The nth element equals the sum of the previous 2 elements.
13
+ return fibonacciRecursive ( nthElement - 2 ) + fibonacciRecursive ( nthElement - 1 ) ;
14
+ }
15
+
16
+ // Time Complexity: O(n) Space Complexity: O(1)
17
+ function fibonacciIterative ( nthElement : number ) : number | undefined {
18
+ if ( nthElement < 1 ) return undefined ;
19
+ else nthElement = Math . floor ( nthElement ) ;
20
+
21
+ let previous = 0 ;
22
+ let current = 1 ;
23
+
24
+ if ( nthElement === 1 ) return previous ;
25
+ if ( nthElement === 2 ) return current ;
26
+
27
+ let fibonacci : number = 0 ;
28
+
29
+ for ( let i = 3 ; i <= nthElement ; ++ i ) {
30
+ fibonacci = previous + current ;
31
+ previous = current ;
32
+ current = fibonacci ;
33
+ }
34
+
35
+ return fibonacci ;
36
+ }
37
+
38
+ function validateFibonacciInput ( nthElement : number ) {
39
+ return nthElement > 0 ;
40
+ }
41
+
42
+ function printFibonacciIterative ( nthElement : number ) {
43
+ console . log ( "Fibonacci Iterative" , nthElement + ':' , fibonacciIterative ( nthElement ) ) ;
44
+ }
45
+
46
+ function printFibonacciRecursive ( nthElement : number ) {
47
+ if ( ! validateFibonacciInput ( nthElement ) ) {
48
+ console . log ( "Input" , nthElement , "is invalid -_-'" ) ;
49
+ return ;
50
+ }
51
+ console . log ( "Fibonacci Recursive" , nthElement + ':' , fibonacciRecursive ( nthElement ) ) ;
52
+ }
53
+
54
+ //---------------------------------------------------------------------
55
+ // ---------- MAIN PROGRAM ----------
56
+ //---------------------------------------------------------------------
57
+ if ( import . meta. main ) {
58
+
59
+ console . log ( '------------ Iterative ------------' ) ;
60
+ printFibonacciIterative ( 0 ) ;
61
+ printFibonacciIterative ( 1 ) ;
62
+ printFibonacciIterative ( 2 ) ;
63
+ printFibonacciIterative ( 3 ) ;
64
+ printFibonacciIterative ( 4 ) ;
65
+ printFibonacciIterative ( 5 ) ;
66
+ printFibonacciIterative ( 6 ) ;
67
+ printFibonacciIterative ( 9 ) ;
68
+ printFibonacciIterative ( 42 ) ;
69
+ printFibonacciIterative ( 50 ) ;
70
+ printFibonacciIterative ( 100 ) ;
71
+ console . log ( '\n------------ Recursive ------------' ) ;
72
+ printFibonacciRecursive ( - 1 ) ;
73
+ printFibonacciRecursive ( 0 ) ;
74
+ printFibonacciRecursive ( 1 ) ;
75
+ printFibonacciRecursive ( 2 ) ;
76
+ printFibonacciRecursive ( 3 ) ;
77
+ printFibonacciRecursive ( 4 ) ;
78
+ printFibonacciRecursive ( 5 ) ;
79
+ printFibonacciRecursive ( 7 ) ;
80
+ printFibonacciRecursive ( 8 ) ;
81
+ printFibonacciRecursive ( 42 ) ; // Notice how much longer this takes than the iterative version
82
+
83
+ // RUN: deno run Algorithms/Recursion/Fibonacci.ts
84
+ }
85
+
86
+ // --------------------------- Terminal Output: ---------------------------
87
+ // ------------ Iterative ------------
88
+ // Fibonacci Iterative 0: undefined
89
+ // Fibonacci Iterative 1: 0
90
+ // Fibonacci Iterative 2: 1
91
+ // Fibonacci Iterative 3: 1
92
+ // Fibonacci Iterative 4: 2
93
+ // Fibonacci Iterative 5: 3
94
+ // Fibonacci Iterative 6: 5
95
+ // Fibonacci Iterative 9: 21
96
+
97
+ // ------------ Recursive ------------
98
+ // Input -1 is invalid -_-'
99
+ // Input 0 is invalid -_-'
100
+ // Fibonacci Recursive 1: 0
101
+ // Fibonacci Recursive 2: 1
102
+ // Fibonacci Recursive 3: 1
103
+ // Fibonacci Recursive 4: 2
104
+ // Fibonacci Recursive 5: 3
105
+ // Fibonacci Recursive 7: 8
106
+ // Fibonacci Recursive 8: 13
0 commit comments