Skip to content

Commit a55a0c9

Browse files
committed
ES6 implementation of Factorial and Fibonacci added
1 parent 66ed911 commit a55a0c9

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Factorial Calculation in JavaScript(ES6)
3+
-----------------------------------
4+
5+
As Factorial numbers can be calculated using the formula:
6+
Factorial[n] = 1 * 2 * 3 * ...... * n
7+
We can easily calculate n-th factorial number multiplying
8+
all the previous numbers.
9+
10+
NOTE: remember that, the Factorial[N] can be very large
11+
for a very small value of N. So always be careful about
12+
the result being overflowed of Number limit.
13+
Calculating large values of Factorial needs some extra
14+
optimization and tricks based on the task to be solved.
15+
*/
16+
17+
const factorial = (n) => {
18+
let fact = 1;
19+
20+
while(n) {
21+
fact *= n;
22+
n--;
23+
}
24+
25+
return fact;
26+
}
27+
28+
29+
/*
30+
Solving Factorial using recursion.
31+
Recursive formula for Factorial is: F[n] = n * F[n-1]
32+
*/
33+
const recursiveFactorial = (n) => (n === 0) ? 1 : (n * recursiveFactorial(n-1));
34+
35+
/* Following is an optimized version of calculating Factorial using 'Memoization' or
36+
'Dynamic Programming'.
37+
To understand how this is actually working you need to read the following article first:
38+
https://medium.freecodecamp.org/understanding-memoize-in-javascript-51d07d19430e
39+
*/
40+
41+
const memoizedFunction = (fn) => {
42+
var cache = {};
43+
44+
return (n) => (n in cache) ? cache[n] : (cache[n] = fn(n));
45+
}
46+
47+
const memoizedFactorial = memoizedFunction((x) => (x === 0) ? 1 : (x * memoizedFactorial(x-1)));
48+
49+
module.exports = {
50+
factorial,
51+
recursiveFactorial,
52+
memoizedFactorial
53+
}

Number Theory/Factorial/es6/test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const { factorial, recursiveFactorial, memoizedFactorial } = require('./factorial')
2+
3+
console.log('====== Simple Factorial ==========');
4+
console.log(factorial(1));
5+
console.log(factorial(2));
6+
console.log(factorial(3));
7+
console.log(factorial(4));
8+
console.log(factorial(5));
9+
console.log(factorial(10));
10+
console.log(factorial(20));
11+
12+
console.log('====== Recursive Factorial ==========');
13+
console.log(recursiveFactorial(10));
14+
console.log(recursiveFactorial(20));
15+
16+
console.log('====== Memoized Factorial ==========');
17+
console.log(memoizedFactorial(5));
18+
console.log(memoizedFactorial(6));
19+
console.log(memoizedFactorial(5));
20+
console.log(memoizedFactorial(10));
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Fibonacci Calculation in JavaScript(ES6)
3+
-----------------------------------
4+
5+
As fibonacci numbers follow the pattern of:
6+
Fib[n] = Fib[n-1] + Fib[n-2]
7+
(having the base case of starting values-> Fib[0] = 0 and Fib[1] = 1)
8+
We can easily calculate n-th fibonacci number calculating
9+
all the previous values by this formula.
10+
11+
NOTE: remember that, the Fibonacci[N] can be very large
12+
for a very small value of N (You can safely calculate almost upto N=97).
13+
So always be careful about the result being overflowed of Number limit.
14+
Calculating large values of Fibonacci needs some extra
15+
optimization and tricks based on the task to be solved.
16+
*/
17+
18+
const fibonacci = (n) => {
19+
let fib, a = 0, b = 1;
20+
21+
if(n === 0 || n === 1) return n;
22+
23+
while(--n) {
24+
fib = a + b;
25+
a = b;
26+
b = fib;
27+
}
28+
29+
return fib;
30+
}
31+
32+
/*
33+
Solving Fibonacci using recursion.
34+
Recursive formula for Fibonacci is: F[n] = F[n-1] + F[n-2]
35+
*/
36+
const recursiveFibonacci = (n) => (n === 0 || n === 1) ? n : (recursiveFibonacci(n-1) + recursiveFibonacci(n-2));
37+
38+
/* Following is an optimized version of calculating Fibonacci using 'Memoization' or
39+
'Dynamic Programming'.
40+
To understand how this is actually working you need to read the following article first:
41+
https://medium.freecodecamp.org/understanding-memoize-in-javascript-51d07d19430e
42+
*/
43+
44+
const memoizedFunction = (fn) => {
45+
var cache = {};
46+
47+
return (n) => (n in cache) ? cache[n] : (cache[n] = fn(n));
48+
}
49+
50+
const memoizedFibonacci = memoizedFunction((x) => (x === 0 || x === 1) ? x : (memoizedFibonacci(x - 1) + memoizedFibonacci(x - 2)));
51+
52+
module.exports = {
53+
fibonacci,
54+
recursiveFibonacci,
55+
memoizedFibonacci
56+
}

Number Theory/Fibonacci/es6/test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const { fibonacci, recursiveFibonacci, memoizedFibonacci } = require('./fibonacci')
2+
3+
console.log('====== Simple Fibonacci ==========');
4+
console.log(fibonacci(1));
5+
console.log(fibonacci(2));
6+
console.log(fibonacci(3));
7+
console.log(fibonacci(4));
8+
console.log(fibonacci(5));
9+
console.log(fibonacci(10));
10+
11+
console.log('====== Recursive Fibonacci ==========');
12+
console.log(recursiveFibonacci(5));
13+
console.log(recursiveFibonacci(10));
14+
15+
console.log('====== Memoized Fibonacci ==========');
16+
console.log(memoizedFibonacci(5));
17+
console.log(memoizedFibonacci(6));
18+
console.log(memoizedFibonacci(10));

0 commit comments

Comments
 (0)