-
Notifications
You must be signed in to change notification settings - Fork 31
/
factorial.js
85 lines (67 loc) · 2.15 KB
/
factorial.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
/*
Factorial Calculation in JavaScript
-----------------------------------
As Factorial numbers can be calculated using the formula:
Factorial[n] = 1 * 2 * 3 * ...... * n
We can easily calculate n-th factorial number multiplying
all the previous numbers.
NOTE: remember that, the Factorial[N] can be very large
for a very small value of N. So always be careful about
the result being overflowed of Number limit.
Calculating large values of Factorial needs some extra
optimization and tricks based on the task to be solved.
*/
function factorial(n) {
var fact = 1;
while(n) {
fact *= n;
n--;
}
return fact;
}
/*
Solving Factorial using recursion.
Recursive formula for Factorial is: F[n] = n * F[n-1]
*/
function recursiveFactorial(n) {
if (n === 0) return 1;
return n * recursiveFactorial(n-1);
}
/* Following is an optimized version of calculating Factorial using 'Memoization' or
'Dynamic Programming'.
To understand how this is actually working you need to read the following article first:
https://medium.freecodecamp.org/understanding-memoize-in-javascript-51d07d19430e
*/
function memoizedFunction(fn) {
var cache = {};
return function(n) {
if (n in cache) return cache[n];
cache[n] = fn(n);
return cache[n];
}
}
var memoizedFactorial = memoizedFunction(function(x) {
if (x === 0) {
return 1;
}
else {
return x * memoizedFactorial(x - 1); //Recursive formula for factorial is: F[n] = n * F[n-1]
}
});
/************ Testing Factorial ***************/
console.log('====== Simple Factorial ==========');
console.log(factorial(1));
console.log(factorial(2));
console.log(factorial(3));
console.log(factorial(4));
console.log(factorial(5));
console.log(factorial(10));
console.log(factorial(20));
console.log('====== Recursive Factorial ==========');
console.log(recursiveFactorial(10));
console.log(recursiveFactorial(20));
console.log('====== Memoized Factorial ==========');
console.log(memoizedFactorial(5));
console.log(memoizedFactorial(6));
console.log(memoizedFactorial(5));
console.log(memoizedFactorial(10));