Skip to content

Commit 77e897b

Browse files
committed
Add factorial.
1 parent 4434e96 commit 77e897b

File tree

5 files changed

+95
-1
lines changed

5 files changed

+95
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
### Algorithms
2929

3030
* **Math**
31+
* [Factorial](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/factorial)
3132
* [Fibonacci Number](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/fibonacci)
3233
* [Cartesian Product](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/cartesian-product)
3334
* [Power Set](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/power-set)
@@ -37,7 +38,7 @@
3738
* [Fisher–Yates Shuffle](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/fisher-yates) - random permutation of a finite sequence
3839
* **String**
3940
* [Permutations](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/permutations) (with and without repetitions)
40-
* Combination
41+
* [Combinations](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/combinations) (with and without repetitions)
4142
* Minimum Edit distance (Levenshtein Distance)
4243
* Hamming
4344
* Huffman
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Factorial
2+
3+
In mathematics, the factorial of a non-negative integer `n`,
4+
denoted by `n!`, is the product of all positive integers less
5+
than or equal to `n`. For example:
6+
7+
```
8+
5! = 5 * 4 * 3 * 2 * 1 = 120
9+
```
10+
11+
| n | n! |
12+
| ----- | :-------------------------: |
13+
| 0 | 1 |
14+
| 1 | 1 |
15+
| 2 | 2 |
16+
| 3 | 6 |
17+
| 4 | 24 |
18+
| 5 | 120 |
19+
| 6 | 720 |
20+
| 7 | 5 040 |
21+
| 8 | 40 320 |
22+
| 9 | 362 880 |
23+
| 10 | 3 628 800 |
24+
| 11 | 39 916 800 |
25+
| 12 | 479 001 600 |
26+
| 13 | 6 227 020 800 |
27+
| 14 | 87 178 291 200 |
28+
| 15 | 1 307 674 368 000 |
29+
30+
## References
31+
32+
[Wikipedia](https://en.wikipedia.org/wiki/Factorial)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import factorial from '../factorial';
2+
3+
describe('factorial', () => {
4+
it('should calculate factorial', () => {
5+
expect(factorial(0)).toBe(1);
6+
expect(factorial(1)).toBe(1);
7+
expect(factorial(5)).toBe(120);
8+
expect(factorial(8)).toBe(40320);
9+
expect(factorial(10)).toBe(3628800);
10+
});
11+
});
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @param {number} number
3+
* @return {number}
4+
*/
5+
export default function factorial(number) {
6+
let result = 1;
7+
8+
for (let i = 1; i <= number; i += 1) {
9+
result *= i;
10+
}
11+
12+
return result;
13+
}

src/algorithms/string/combinations/README.md

+37
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,45 @@ its the same fruit salad.
1111

1212
## Combinations without repetitions
1313

14+
This is how lotteries work. The numbers are drawn one at a
15+
time, and if we have the lucky numbers (no matter what order)
16+
we win!
17+
18+
No Repetition: such as lottery numbers `(2,14,15,27,30,33)`
19+
20+
**Number of combinations**
21+
22+
![Formula](https://www.mathsisfun.com/combinatorics/images/combinations-no-repeat.png)
23+
24+
where `n` is the number of things to choose from, and we choose `r` of them,
25+
no repetition, order doesn't matter.
26+
27+
It is often called "n choose r" (such as "16 choose 3"). And is also known as the Binomial Coefficient.
28+
1429
## Combinations with repetitions
1530

31+
Repetition is Allowed: such as coins in your pocket `(5,5,5,10,10)`
32+
33+
Or let us say there are five flavours of icecream:
34+
`banana`, `chocolate`, `lemon`, `strawberry` and `vanilla`.
35+
36+
We can have three scoops. How many variations will there be?
37+
38+
Let's use letters for the flavours: `{b, c, l, s, v}`.
39+
Example selections include:
40+
41+
- `{c, c, c}` (3 scoops of chocolate)
42+
- `{b, l, v}` (one each of banana, lemon and vanilla)
43+
- `{b, v, v}` (one of banana, two of vanilla)
44+
45+
**Number of combinations**
46+
47+
![Formula](https://www.mathsisfun.com/combinatorics/images/combinations-repeat.gif)
48+
49+
Where `n` is the number of things to choose from, and we
50+
choose `r` of them. Repetition allowed,
51+
order doesn't matter.
52+
1653
## References
1754

1855
[Math Is Fun](https://www.mathsisfun.com/combinatorics/combinations-permutations.html)

0 commit comments

Comments
 (0)