This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let xn = (x, n) => x ** (n - 1n); // TODO: has a probably O(n!) complexity. More efficient algorithm needed. | |
function mod(x, n) { | |
const _xn = xn(x, n); | |
return (_xn - 1n) % n === 0n; | |
} | |
function withTimer(fn, timeName = 'Run time') { | |
return (...args) => { | |
console.time(timeName); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function test() { | |
assert.shouldEqual(factorial(0), 1); | |
assert.shouldEqual(factorial(1), 1); | |
assert.shouldEqual(factorial(2), 2); | |
assert.shouldEqual(factorial(6), 1); | |
assert.shouldEqual(factorial(10), 3628800); | |
assert.shouldEqual(factorial(12), 479001600); | |
assert.shouldEqual(factorial(18), 6402373705728000); | |
// Throw error for factorials greater than 18 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
/** | |
* Flattens a deep-nested array of integers (numbers) | |
* @param {Number[]} inpArray A possibly deep-nested array of numbers. | |
* The level of nesting is limited by the maximum stack size of the | |
* JavaScript engine (the lowest we have seen is IE6 at 1130) | |
* @return {Number[]} Returns a flattened version of the input array | |
*/ | |
function flatten (inpArray) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (var i = 0; i < 1024 * 1024; i++) { | |
process.nextTick(function () { Math.sqrt(i) } ) | |
} |