-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursion.js
More file actions
189 lines (153 loc) · 4.15 KB
/
recursion.js
File metadata and controls
189 lines (153 loc) · 4.15 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//! ====================================================
// calculate mathematical expression using recursion function.
//! ====================================================
const num1 = 2;
const num2 = 3;
function pow(base, exponent) {
if (exponent === 0) {
return 1;
}
return base * pow(base, exponent - 1);
}
// console.log(`The pow of ${num1} raised to ${num2} is ${pow(num1, num2)}`);
// Output: The pow of 2 raised to 3 is 8
//! ====================================================
// factorial using Recursion function
//! ====================================================
const number = 5;
function factorial(num) {
if (num < 1) {
return 1;
}
return num * factorial(num - 1);
}
// console.log(` the factorial of number ${number} is ${factorial(number)}`);
// the factorial of number 5 is 120
//! ====================================================
// fibonacci sequence
//! ====================================================
let number1 = 10;
function fibonacci(num) {
if (num === 0) {
return 0;
}
if (num === 1) {
return 1;
}
return fibonacci(num - 1) + fibonacci(num - 2);
}
// console.log(`Fibonacci Seq of given number ${number1}: ${fibonacci(number1)}`);
// Fibonacci Seq of given number 10: 55
for (let i = 0; i <= number1; i++) {
// console.log(fibonacci(i));
}
// 0
// 1
// 1
// 2
// 3
// 5
// 8
// 13
// 21
// 34
// 55
//! ====================================================
// Linear Sum
//! ====================================================
const number2 = 10;
function sum(num) {
if (num === 0) {
return 0;
}
return num + sum(num - 1);
}
// console.log(sum(number2));// 55
let linearSum = [];
for (let i = 0; i < number2; i++) {
linearSum.push(sum(i));
}
// console.log(linearSum);
// [
// 0, 1, 3, 6, 10,
// 15, 21, 28, 36, 45
// ]
//! ====================================================
// Count number of digits
//! ====================================================
const Number4 = 12345;
function countNumDigit(num) {
if (parseInt(num) < 1) {
return 0;
}
return 1 + countNumDigit(parseInt(num) / 10);
}
// console.log(`Total Number of digits in the given number ${Number4} is : ${countNumDigit(Number4)}`);
// Total Number of digits in the given number 12345 is : 5
//! ====================================================
// Find Gcd And Lcm
//! ====================================================
function gcd(a, b) {
if (b === 0) {
return a;
}
return gcd(b, a % b);
}
function lcm(a, b) {
return Math.floor(a * b) / gcd(a, b);
}
const num5 = 48;
const num6 = 18;
// console.log(`GCD of ${num1} and ${num2} is: ${gcd(num1, num2)}`);
// GCD of 2 and 3 is: 1
// console.log(`LCM of ${num1} and ${num2} is: ${lcm(num1, num2)}`);
// LCM of 2 and 3 is: 6
//! ====================================================
// Check if a String is a Palindrome
//! ====================================================
let str1 = "HELEH";
function isPalindrome(str, start, end) {
if (start >= end) {
return true;
}
if (str[start] !== str[end]) {
return false;
}
return isPalindrome(str, start + 1, end - 1);
}
// console.log(isPalindrome(str1, 0, str1.length - 1));
// true
//! ====================================================
// Linear Sum odd number
//! ====================================================
let num8 = 10;
function LinearSumOddNumber(num) {
if (num <= 0) {
return 0;
} else if (num % 2 != 0) {
return num + LinearSumOddNumber(num - 2);
} else {
return LinearSumOddNumber(num - 1);
}
}
// console.log(LinearSumOddNumber(num8)); // 25
//! ====================================================
// Linear Sum of even number
//! ====================================================
const number3 = 10;
function LinearSumOfEvenNum(num) {
if (num <= 0) {
return 0;
} else if (num % 2 === 0) {
return num + LinearSumOfEvenNum(num - 2);
} else {
return LinearSumOfEvenNum(num - 1);
}
}
// console.log(LinearSumOfEvenNum(number3)); // Output: 30
let linearEvenSum = [];
for (let i = 0; i <= number3; i++) {
linearEvenSum.push(LinearSumOfEvenNum(i));
}
// console.log(linearEvenSum);
// [0, 0, 2, 2, 6, 6, 12, 12, 20, 20, 30]