Skip to content
Partition a number, x, into n partitions of random values.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
test
.gitignore
LICENSE
README.md
number-partition.js
package-lock.json
package.json

README.md

number-partition

Partition a natural number, x, into n randomly-sized partitions such that the sum of the partitions is equal to x.

Install

npm install number-partition

Syntax

This function is curried by default.

const partitions = partition(n)(x);

Parameters

n (number)
The number of partitions to generate.

x (number)
The number to partition.

Usage

const partition = require('number-partition');

partition(3)(100);
// [4, 54, 42]

partition(3)(100);
// [53, 37, 10]

partition(5)(190);
// [108, 23, 43, 1, 15]

partition(5)(190);
// [ 9, 4, 115, 47, 15 ]

const intoFive = partition(5);

intoFive(512);
// [ 488, 18, 2, 2, 2 ]

intoFive(512);
// [ 281, 83, 77, 18, 53 ]

Running tests

npm install
npm test

The tests are written with Mocha using Chai as an assertion library.

License

WTFPL

You can’t perform that action at this time.