Add factorial.

This commit is contained in:
Oleksii Trekhleb
2018-04-18 13:04:05 +03:00
parent 4434e96413
commit 77e897b3b9
5 changed files with 95 additions and 1 deletions

View File

@ -0,0 +1,32 @@
# Factorial
In mathematics, the factorial of a non-negative integer `n`,
denoted by `n!`, is the product of all positive integers less
than or equal to `n`. For example:
```
5! = 5 * 4 * 3 * 2 * 1 = 120
```
| n | n! |
| ----- | :-------------------------: |
| 0 | 1 |
| 1 | 1 |
| 2 | 2 |
| 3 | 6 |
| 4 | 24 |
| 5 | 120 |
| 6 | 720 |
| 7 | 5 040 |
| 8 | 40 320 |
| 9 | 362 880 |
| 10 | 3 628 800 |
| 11 | 39 916 800 |
| 12 | 479 001 600 |
| 13 | 6 227 020 800 |
| 14 | 87 178 291 200 |
| 15 | 1 307 674 368 000 |
## References
[Wikipedia](https://en.wikipedia.org/wiki/Factorial)

View File

@ -0,0 +1,11 @@
import factorial from '../factorial';
describe('factorial', () => {
it('should calculate factorial', () => {
expect(factorial(0)).toBe(1);
expect(factorial(1)).toBe(1);
expect(factorial(5)).toBe(120);
expect(factorial(8)).toBe(40320);
expect(factorial(10)).toBe(3628800);
});
});

View File

@ -0,0 +1,13 @@
/**
* @param {number} number
* @return {number}
*/
export default function factorial(number) {
let result = 1;
for (let i = 1; i <= number; i += 1) {
result *= i;
}
return result;
}