-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter9.js
More file actions
78 lines (73 loc) · 1.75 KB
/
chapter9.js
File metadata and controls
78 lines (73 loc) · 1.75 KB
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
// ################ Recursive Sigma ################################
function rsigma(num, sum = 0, step = 0){
if (num != step){
step ++;
sum = sum + step;
return rsigma(num, sum, step);
}
return sum;
}
console.log(rsigma(5))
// ################ Flood Fill ######################################
Function floodFill(canvas2D, startPos, newColor)
if (canvas2D[Y][X-1] == startPos){
canvas2D[Y][X-1] == newColor;
floodFill(canvas2D, [Y, X-1], newColor;
}
if (canvas2D[Y][X+1] == startPos){
canvas2D[Y][X+1] == newColor;
floodFill(canvas2D, [Y, X+1], newColor;
}
if (canvas2D[X][Y-1] == startPos){
canvas2D[X][Y-1] == newColor;
floodFill(canvas2D, [X, Y-1], newColor;
}
if (canvas2D[X][Y+1] == startPos){
canvas2D[X][Y+1] == newColor;
floodFill(canvas2D, [X, Y+1], newColor;
}
canvas2D =
[[3, 2, 3, 4 ,5]
[2, 3, 3, 4, 0]
[7, 3, 3, 5, 3]
[6, 5, 3, 4, 1]
[1, 2, 3, 3, 3]]
// ################# Recursive Fibonacci ##########################
function rFib(num){
if(num == 2){
return 1;
}
if(num <= 1){
return 0;
}
return rFib(num -1) + rfib(num - 2);
}
rFib(21)
// ################# Recursive Tribonacci ###############################
function rTrib(num){
if(num <= 2){
return 0;
}
return rTrib(num -3) + rTrib(num - 2) + rTrib(num -1);
}
rTrib(24)
//################# Recursive Binary Search ################################
function rBinarySearch(arr, val, start = 0, end = -1){
if(end == -1){
end = arr.length;
}
mid = Math.floor((end + start)/2);
if(val == arr[mid]){
return true;
}
if(start == end){
return false;
}
if(val < arr[mid]){
return rBinarySearch(arr, val, start, mid);
}
if(val > arr[mid]){
return rBinarySearch(arr, val, mid, end);
}
}
rBinarySearch([2,4,6,12,21,32,40], 32, start = 0, end = -1)