pythagorean-triples
Install
$ npm install pythagorean-triples
var triples = ;
Usage
triples.euclid(m, n)
Generates a Pythagorean triple, using Euclid's formula a = m^2 - n^2, b = 2mn, c = m^2 + n^2
.
The triple is sorted numerically a < b < c
.
console; // [3, 4, 5]console; // [5, 12, 13]
triples.upToM(m)
Generates all the triples for integers m
and n
where 1 <= n < m
.
console;
3 4 5 6 8 10 5 12 13 8 15 17 12 16 20 7 24 25
triples.upToSum(sum)
Generates all the triples where a + b + c <= sum
.
console
3 4 5 6 8 10 5 12 13
triples.isTriple(triple)
Checks if the given triple is a valid triple:
- array with three values
- all values positive integers
- can make a right angle:
a^2 + b^2 = c^2
console
triples.isPrimitive(triple)
Checks if the given triple is primitive. A triple is primitive if all values are coprime, which means their greatest common divisor is 1.
console
Tests
To run the test suite, first install the dependencies, then run npm test
:
$ npm install
$ npm test