-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patharithmetic-operations.js
41 lines (36 loc) · 1006 Bytes
/
arithmetic-operations.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
console.log('~~~~~~~ Sum ~~~~~~~~');
/**
* Time Complexity : O(n)
* Space Complexity: O(n)
*/
const sumRange = (uptoNum) => {
console.log('uptoNum',uptoNum);
if(uptoNum === 1) return 1;
return uptoNum + sumRange(uptoNum -1);
}
console.log(sumRange(2));
console.log('~~~~~~~~~~~~~~~~~~~~~')
console.log(sumRange(3));
console.log('~~~~~~~~~~~~~~~~~~~~~')
console.log(sumRange(4));
console.log('~~~~~~~~~~~~~~~~~~~~~')
console.log(sumRange(5));
console.log('~~~~~~~~~~~~~~~~~~~~~')
console.log('~~~~~~~ Factorial ~~~~~~~~');
console.log('~~~~~~~~~~~~~~~~~~~~~')
/**
* Time Complexity : O(n)
* Space Complexity: O(n)
*/
const factorial = (uptoNum) => {
console.log('uptoNum',uptoNum);
if(uptoNum === 1) return 1;
return uptoNum * factorial(uptoNum -1);
}
console.log(factorial(2));
console.log('~~~~~~~~~~~~~~~~~~~~~')
console.log(factorial(3));
console.log('~~~~~~~~~~~~~~~~~~~~~')
console.log(factorial(4));
console.log('~~~~~~~~~~~~~~~~~~~~~')
console.log(factorial(5));