Skip to content

reity/summations-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

summations-js

Library to enumerate all natural number lists with a target sum.

npm version

Package Installation and Usage

The package is available on npm:

npm install summations

The library can be imported in the usual ways:

var summations = require('summations');

The library also supports standalone usage in browsers:

<script src="https://reity.org/summations-js/lib/summations.js"></script>

Examples

It is possible to enumerate all possible lists of natural numbers of a given length that add up to a target sum.

var results = summations.sumLen(2, 3);

Given the object above, we can query it for each value

> results
[
  [1, 1, 0],
  [0, 1, 1],
  [1, 0, 1],
  [2, 0, 0],
  [0, 2, 0],
  [0, 0, 2],
]

Testing

Unit tests are included in test/test.js. They can be run using Mocha:

npm test