combinations-js
v1.1.2
Published
n choose k for Node.js
Downloads
151
Readme
combinations.js
Find the amount of possible combinations of n elements
Background
Combinatorics is a part of discrete mathematics that specializes in finding all possible combinations of data within a set data structures (set, list, array, etc). This library takes the amount of elements, "n", along with the prefered number of elements chosen from the array, "k", and outputs the total number of possible combinations of n involving k elements.
Basically, it's doing this:
as long as k < n.
We call this n choose k.
Example
We are given the set of {1, 2, 3, 4, 5}. We want to know how many unique sets there are containing three elements. Therefore, we use the combination formula to find out.
In other words, we are finding 5 choose 3.
({1,2,3}, {1,2,4}, {1,2,5}, {1,3,4}, {1,3,5}, {1,4,5}, {2,3,4}, {2,3,5}, {2,4,5}, {3,4,5})
The answer is 10 combinations.
Usage
Installing
npm install --save combinations-js
Importing
var combinations = require('combinations-js');
var answer = combinations(n, k);