diff --git a/README.md b/README.md index b8c50e82..62650ba8 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ ### Algorithms * **Math** + * [Factorial](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/factorial) * [Fibonacci Number](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/fibonacci) * [Cartesian Product](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/cartesian-product) * [Power Set](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/power-set) @@ -37,7 +38,7 @@ * [Fisher–Yates Shuffle](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/fisher-yates) - random permutation of a finite sequence * **String** * [Permutations](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/permutations) (with and without repetitions) - * Combination + * [Combinations](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/combinations) (with and without repetitions) * Minimum Edit distance (Levenshtein Distance) * Hamming * Huffman diff --git a/src/algorithms/math/factorial/README.md b/src/algorithms/math/factorial/README.md new file mode 100644 index 00000000..06400c55 --- /dev/null +++ b/src/algorithms/math/factorial/README.md @@ -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) diff --git a/src/algorithms/math/factorial/__test__/factorial.test.js b/src/algorithms/math/factorial/__test__/factorial.test.js new file mode 100644 index 00000000..bf6aa0ec --- /dev/null +++ b/src/algorithms/math/factorial/__test__/factorial.test.js @@ -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); + }); +}); diff --git a/src/algorithms/math/factorial/factorial.js b/src/algorithms/math/factorial/factorial.js new file mode 100644 index 00000000..194fc898 --- /dev/null +++ b/src/algorithms/math/factorial/factorial.js @@ -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; +} diff --git a/src/algorithms/string/combinations/README.md b/src/algorithms/string/combinations/README.md index a4d58408..517a7f33 100644 --- a/src/algorithms/string/combinations/README.md +++ b/src/algorithms/string/combinations/README.md @@ -11,8 +11,45 @@ its the same fruit salad. ## Combinations without repetitions +This is how lotteries work. The numbers are drawn one at a +time, and if we have the lucky numbers (no matter what order) +we win! + +No Repetition: such as lottery numbers `(2,14,15,27,30,33)` + +**Number of combinations** + +![Formula](https://www.mathsisfun.com/combinatorics/images/combinations-no-repeat.png) + +where `n` is the number of things to choose from, and we choose `r` of them, +no repetition, order doesn't matter. + +It is often called "n choose r" (such as "16 choose 3"). And is also known as the Binomial Coefficient. + ## Combinations with repetitions +Repetition is Allowed: such as coins in your pocket `(5,5,5,10,10)` + +Or let us say there are five flavours of icecream: +`banana`, `chocolate`, `lemon`, `strawberry` and `vanilla`. + +We can have three scoops. How many variations will there be? + +Let's use letters for the flavours: `{b, c, l, s, v}`. +Example selections include: + +- `{c, c, c}` (3 scoops of chocolate) +- `{b, l, v}` (one each of banana, lemon and vanilla) +- `{b, v, v}` (one of banana, two of vanilla) + +**Number of combinations** + +![Formula](https://www.mathsisfun.com/combinatorics/images/combinations-repeat.gif) + +Where `n` is the number of things to choose from, and we +choose `r` of them. Repetition allowed, +order doesn't matter. + ## References [Math Is Fun](https://www.mathsisfun.com/combinatorics/combinations-permutations.html)