Avatar of brunnock

brunnock's solution

to Pythagorean Triplet in the JavaScript Track

Published at Jul 13 2018 · 0 comments
Test suite


This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

A Pythagorean triplet is a set of three natural numbers, {a, b, c}, for which,

a**2 + b**2 = c**2

For example,

3**2 + 4**2 = 9 + 16 = 25 = 5**2.

There exists exactly one Pythagorean triplet for which a + b + c = 1000.

Find the product a * b * c.


Go through the setup instructions for JavaScript to install the necessary dependencies:


Running the test suite

The provided test suite uses Jasmine. You can install it by opening a terminal window and running the following command:

npm install -g jasmine

Run the test suite from the exercise directory with:

jasmine pythagorean-triplet.spec.js

In many test suites all but the first test have been marked "pending". Once you get a test passing, activate the next one by changing xit to it.


Problem 9 at Project Euler http://projecteuler.net/problem=9

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


var Triplet = require('./pythagorean-triplet');

describe('Triplet', function () {
  it('calculates the sum', function () {
    expect(new Triplet(3, 4, 5).sum()).toBe(12);

  xit('calculates the product', function () {
    expect(new Triplet(3, 4, 5).product()).toBe(60);

  xit('can recognize a pythagorean triplet', function () {
    expect(new Triplet(3, 4, 5).isPythagorean()).toBe(true);

  xit('can recognize a non pythagorean triplet', function () {
    expect(new Triplet(5, 6, 7).isPythagorean()).toBe(false);

  xit('can make triplets up to 10', function () {
    var triplets = Triplet.where({ maxFactor: 10 });
    var products = triplets.sort().map( function (triplet) {
      return triplet.product();
    expect(products).toEqual([60, 480]);

  xit('can make triplets 11 through 20', function () {
    var triplets = Triplet.where({ minFactor: 11, maxFactor: 20 });
    var products = triplets.sort().map( function (triplet) {
      return triplet.product();

  xit('can filter on sum', function () {
    var triplets = Triplet.where({ sum: 180, maxFactor: 100 });
    var products = triplets.sort().map( function (triplet) {
      return triplet.product();
    expect(products).toEqual([118080, 168480, 202500]);
function Triplet(a,b,c) {
  var abc = [a,b,c].sort();
  this.sum = function(){ return a+b+c }
  this.product = function(){ return a*b*c }
  this.isPythagorean = function(){ return (c*c==a*a+b*b) }

Triplet.where = function(args){
  var triplets=[];
  for (var c=args.maxFactor; c>=(args.minFactor||5); --c) {
    for (var b=c-1; b>=(args.minFactor||c/2); --b) {
      var a = Math.sqrt(c*c - b*b);
      if ((a<b) && (a>=args.minFactor) && (a==parseInt(a))) {
	if (args.sum) {
	  if ((a+b+c)==args.sum) triplets.push(new Triplet(a,b,c));
	} else {
	  triplets.unshift(new Triplet(a,b,c));
  return triplets;


Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?