forked from rohan-paul/Awesome-JavaScript-Interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperformance-testing-1.js
33 lines (24 loc) · 949 Bytes
/
performance-testing-1.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
function wallisPi(num) {
var productPi = 1;
for (var i = 1; i <= num; i++) {
productPi *= ((2 * i) / (2 * i - 1)) * ((2 * i) / (2 * i + 1));
}
return 2 * productPi;
}
// console.log(wallisPi(50000)); // returns 3.141435593589838
function wallisPiRecurse(num, iterationNum) {
if (num === 0) {
return 2; // The terminal or tail case of the recursion, when the result from the whole multiplication formulae need to be multiplied by 2; i.e. when num hits zero
}
if (!iterationNum) {
iterationNum = 1;
}
return (Math.pow(iterationNum, 2)) / (Math.pow(iterationNum, 2) - 0.25) * wallisPiRecurse(--num, ++iterationNum)
}
// console.log(wallisPiRecurse(5000)); // returns 3.1414355935899008
console.time("factorial test");
wallisPi(100);
console.timeEnd("factorial test");
console.time("factorialRecursive test");
wallisPiRecurse(1000);
console.timeEnd("factorialRecursive test");