mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-09-20 07:43:04 +08:00
Add factorialBigInt
This commit is contained in:
parent
2b4f12622a
commit
e11827b833
@ -0,0 +1,12 @@
|
|||||||
|
import factorialBigInt from '../factorialBigInt';
|
||||||
|
|
||||||
|
describe('factorialBigInt', () => {
|
||||||
|
it('should calculate factorial', () => {
|
||||||
|
expect(factorialBigInt(0n)).toBe(1n);
|
||||||
|
expect(factorialBigInt(1n)).toBe(1n);
|
||||||
|
expect(factorialBigInt(5n)).toBe(120n);
|
||||||
|
expect(factorialBigInt(8n)).toBe(40320n);
|
||||||
|
expect(factorialBigInt(10n)).toBe(3628800n);
|
||||||
|
expect(factorialBigInt(100n)).toBe(93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000n);
|
||||||
|
});
|
||||||
|
});
|
13
src/algorithms/math/factorial/factorialBigInt.js
Normal file
13
src/algorithms/math/factorial/factorialBigInt.js
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
/**
|
||||||
|
* @param {bigint} number
|
||||||
|
* @return {bigint}
|
||||||
|
*/
|
||||||
|
export default function factorialBigInt(number) {
|
||||||
|
let result = 1n;
|
||||||
|
|
||||||
|
for (let i = 2n; i <= number; i += 1n) {
|
||||||
|
result *= i;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user