forked from ManspergerMichael/Algorithims
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecursion.js
96 lines (90 loc) · 2.29 KB
/
Recursion.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//Recursive sigma
//take a given number and add each interger value from n down to 1 to result
function sigma(number){
//fast fail
if(number == 0){
return 0;
}
//if number is not even, round down
if(number % 2 != 0){
number = Math.floor(number);
}
//base case
if(number == 1){
return number;
}
//add result of recursive call to number
else{
return (number + sigma(number - 1));
}
}
//console.log(sigma(5));
//Recurseive factorial
function factorial(number){
//fast fail
if(number == 0){
return 1;
}
//round each calulation down to integer
if(number % 2 != 0){
number = Math.floor(number);
}
//base case
if(number == 1){
return number;
}
else{
return (number * factorial(number - 1));
}
}
//console.log(factorial(6));
//non-recursive fibonacci sequence
//with O(n) time complexity
function fibonacciLoop(num){
//a fibonachi sequence starts with 0 and 1
var a = 1, b = 0, temp;
//loops through calculation until num reaches 0
while (num >= 0){
//store a in temp
temp = a;
//add the two previous numbers to make the next number in the sequence
a = a + b;
//store the previous number in b
b = temp;
//reduce number by one
num--;
}
return b;
}
//console.log(fibonacciLoop(5));
//recursive fibonachi sequence without memoizeation
//Time complexity is O(2^n)
function fibonacciRecursion(num){
num = Math.trunc(num);
if (num == 1) {
return 1;
}
if (num == 0){
return 0;
}
return fibonacciRecursion(num - 1) + fibonacciRecursion(num - 2);
}
//console.log(fibonacciRecursion(4));
//add together previous three values in the sequence
// Tribonacci(3) = (0+0+1)=1
function tribonacci(num){
num = Math.trunc(num);
//base case for tribonachi
if(num == 2){return 1;}
if(num == 1){return 0;}
if(num <= 0){return 0;}
//console.log(num);
return tribonacci(num - 1) + tribonacci(num -2) + tribonacci(num - 3);
}
console.log(tribonacci(4));
/*
Notes on Recursion:
The Base Case is simular to the escape condition of a loop
i.e for(var i = 0; I<ARRAY.LENGTH; i++){}
Modify the data at the time of the function call when continuing the "loop"
*/