🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of JuanMa

JuanMa's solution

to Pythagorean Triplet in the Java Track

Published at Aug 19 2020 · 0 comments
Instructions
Test suite
Solution

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

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

and such that,

a < b < c

For example,

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

Given an input integer N, find all Pythagorean triplets for which a + b + c = N.

For example, with N = 1000, there is exactly one Pythagorean triplet for which a + b + c = 1000: {200, 375, 425}.

Setup

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

https://exercism.io/tracks/java/installation

Running the tests

You can run all the tests for an exercise by entering the following in your terminal:

$ gradle test

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the @Ignore("Remove to run test") annotation.

Source

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.

PythagoreanTripletTest.java

import java.util.Arrays;
import java.util.List;
import java.util.Collections;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.Ignore;

public class PythagoreanTripletTest {

    @Test
    public void tripletsWhoseSumIs12() {
        List<PythagoreanTriplet> actual
                = PythagoreanTriplet
                        .makeTripletsList()
                        .withFactorsLessThanOrEqualTo(12)
                        .thatSumTo(12)
                        .build();
        List<PythagoreanTriplet> expected
                = Collections.singletonList(new PythagoreanTriplet(3, 4, 5));
        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void tripletsWhoseSumIs108() {
        List<PythagoreanTriplet> actual
                = PythagoreanTriplet
                        .makeTripletsList()
                        .withFactorsLessThanOrEqualTo(108)
                        .thatSumTo(108)
                        .build();
        List<PythagoreanTriplet> expected
                = Collections.singletonList(new PythagoreanTriplet(27, 36, 45));
        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void tripletsWhoseSumIs1000() {
        List<PythagoreanTriplet> actual
                = PythagoreanTriplet
                        .makeTripletsList()
                        .withFactorsLessThanOrEqualTo(1000)
                        .thatSumTo(1000)
                        .build();
        List<PythagoreanTriplet> expected
                = Collections.singletonList(new PythagoreanTriplet(200, 375, 425));
        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void tripletsWhoseSumIs1001() {
        List<PythagoreanTriplet> actual
                = PythagoreanTriplet
                        .makeTripletsList()
                        .withFactorsLessThanOrEqualTo(1001)
                        .thatSumTo(1001)
                        .build();
        List<PythagoreanTriplet> expected = Collections.emptyList();
        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void tripletsWhoseSumIs90() {
        List<PythagoreanTriplet> actual
                = PythagoreanTriplet
                        .makeTripletsList()
                        .withFactorsLessThanOrEqualTo(90)
                        .thatSumTo(90)
                        .build();
        List<PythagoreanTriplet> expected
                = Arrays.asList(
                        new PythagoreanTriplet(9, 40, 41),
                        new PythagoreanTriplet(15, 36, 39));
        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void tripletsWhoseSumIs840() {
        List<PythagoreanTriplet> actual
                = PythagoreanTriplet
                        .makeTripletsList()
                        .withFactorsLessThanOrEqualTo(840)
                        .thatSumTo(840)
                        .build();
        List<PythagoreanTriplet> expected
                = Arrays.asList(
                        new PythagoreanTriplet(40, 399, 401),
                        new PythagoreanTriplet(56, 390, 394),
                        new PythagoreanTriplet(105, 360, 375),
                        new PythagoreanTriplet(120, 350, 370),
                        new PythagoreanTriplet(140, 336, 364),
                        new PythagoreanTriplet(168, 315, 357),
                        new PythagoreanTriplet(210, 280, 350),
                        new PythagoreanTriplet(240, 252, 348));
        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void tripletsWhoseSumIs30000() {
        List<PythagoreanTriplet> actual
                = PythagoreanTriplet
                        .makeTripletsList()
                        .withFactorsLessThanOrEqualTo(30000)
                        .thatSumTo(30000)
                        .build();
        List<PythagoreanTriplet> expected
                = Arrays.asList(
                        new PythagoreanTriplet(1200, 14375, 14425),
                        new PythagoreanTriplet(1875, 14000, 14125),
                        new PythagoreanTriplet(5000, 12000, 13000),
                        new PythagoreanTriplet(6000, 11250, 12750),
                        new PythagoreanTriplet(7500, 10000, 12500));
        assertEquals(expected, actual);
    }

}

src/main/java/PythagoreanTriplet.java

import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class PythagoreanTriplet
{

	private List<PythagoreanTriplet> list = null;

	private int a, b, c;

	public PythagoreanTriplet()
	{
		this.list = new ArrayList<PythagoreanTriplet>();
	}

	public PythagoreanTriplet(int a, int b, int c)
	{
		this.a = a;
		this.b = b;
		this.c = c;
	}

	public boolean equals(Object obj)
	{
		if (obj instanceof PythagoreanTriplet){
			PythagoreanTriplet objPyt = (PythagoreanTriplet) obj;
			return ((this.a == objPyt.a) && (this.b == objPyt.b) && (this.c == objPyt.c));
		}
		return false;
	}

	public static PythagoreanTriplet makeTripletsList()
	{
		PythagoreanTriplet pythagoreanTriplet = new PythagoreanTriplet();
		return pythagoreanTriplet;
	}

	public int sum()
	{
		return (this.a + this.b + this.c);
	}

	public PythagoreanTriplet withFactorsLessThanOrEqualTo(int value)
	{
		IntStream.range(1, value).forEach(i -> this.calculatePythagoreanTripletsWithOneSide(value, i));
		return this;
	}

	private void calculatePythagoreanTripletsWithOneSide(int value, int i)
	{
		IntStream.rangeClosed(i+1, value).forEach(j -> this.validateAndAddToInternalList(i, j));
	}

	private void validateAndAddToInternalList(int i, int j)
	{
		double hyp = Math.hypot(i, j);
		if (hyp == Math.ceil(hyp)){
			this.list.add(new PythagoreanTriplet(i, j, Double.valueOf(hyp).intValue()));
		}
	}

	public PythagoreanTriplet thatSumTo(int sum)
	{
		List<PythagoreanTriplet> result = this.list.stream().filter(pythagoreanTriplet -> (pythagoreanTriplet.sum() == sum)).collect(Collectors.toList());
		this.list.clear();
		this.list.addAll(result);
		return this;
	}

	public List<PythagoreanTriplet> build()
	{
		return this.list;
	}

}

src/test/java/PythagoreanTripletTest.java

import java.util.Arrays;
import java.util.List;
import java.util.Collections;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.Ignore;

public class PythagoreanTripletTest {

    @Test
    public void tripletsWhoseSumIs12() {
        List<PythagoreanTriplet> actual
                = PythagoreanTriplet
                        .makeTripletsList()
                        .withFactorsLessThanOrEqualTo(12)
                        .thatSumTo(12)
                        .build();
        List<PythagoreanTriplet> expected
                = Collections.singletonList(new PythagoreanTriplet(3, 4, 5));
        assertEquals(expected, actual);
    }

    //@Ignore("Remove to run test")
    @Test
    public void tripletsWhoseSumIs108() {
        List<PythagoreanTriplet> actual
                = PythagoreanTriplet
                        .makeTripletsList()
                        .withFactorsLessThanOrEqualTo(108)
                        .thatSumTo(108)
                        .build();
        List<PythagoreanTriplet> expected
                = Collections.singletonList(new PythagoreanTriplet(27, 36, 45));
        assertEquals(expected, actual);
    }

    //@Ignore("Remove to run test")
    @Test
    public void tripletsWhoseSumIs1000() {
        List<PythagoreanTriplet> actual
                = PythagoreanTriplet
                        .makeTripletsList()
                        .withFactorsLessThanOrEqualTo(1000)
                        .thatSumTo(1000)
                        .build();
        List<PythagoreanTriplet> expected
                = Collections.singletonList(new PythagoreanTriplet(200, 375, 425));
        assertEquals(expected, actual);
    }

    //@Ignore("Remove to run test")
    @Test
    public void tripletsWhoseSumIs1001() {
        List<PythagoreanTriplet> actual
                = PythagoreanTriplet
                        .makeTripletsList()
                        .withFactorsLessThanOrEqualTo(1001)
                        .thatSumTo(1001)
                        .build();
        List<PythagoreanTriplet> expected = Collections.emptyList();
        assertEquals(expected, actual);
    }

    //@Ignore("Remove to run test")
    @Test
    public void tripletsWhoseSumIs90() {
        List<PythagoreanTriplet> actual
                = PythagoreanTriplet
                        .makeTripletsList()
                        .withFactorsLessThanOrEqualTo(90)
                        .thatSumTo(90)
                        .build();
        List<PythagoreanTriplet> expected
                = Arrays.asList(
                        new PythagoreanTriplet(9, 40, 41),
                        new PythagoreanTriplet(15, 36, 39));
        assertEquals(expected, actual);
    }

    //@Ignore("Remove to run test")
    @Test
    public void tripletsWhoseSumIs840() {
        List<PythagoreanTriplet> actual
                = PythagoreanTriplet
                        .makeTripletsList()
                        .withFactorsLessThanOrEqualTo(840)
                        .thatSumTo(840)
                        .build();
        List<PythagoreanTriplet> expected
                = Arrays.asList(
                        new PythagoreanTriplet(40, 399, 401),
                        new PythagoreanTriplet(56, 390, 394),
                        new PythagoreanTriplet(105, 360, 375),
                        new PythagoreanTriplet(120, 350, 370),
                        new PythagoreanTriplet(140, 336, 364),
                        new PythagoreanTriplet(168, 315, 357),
                        new PythagoreanTriplet(210, 280, 350),
                        new PythagoreanTriplet(240, 252, 348));
        assertEquals(expected, actual);
    }

    //@Ignore("Remove to run test")
    @Test
    public void tripletsWhoseSumIs30000() {
        List<PythagoreanTriplet> actual
                = PythagoreanTriplet
                        .makeTripletsList()
                        .withFactorsLessThanOrEqualTo(30000)
                        .thatSumTo(30000)
                        .build();
        List<PythagoreanTriplet> expected
                = Arrays.asList(
                        new PythagoreanTriplet(1200, 14375, 14425),
                        new PythagoreanTriplet(1875, 14000, 14125),
                        new PythagoreanTriplet(5000, 12000, 13000),
                        new PythagoreanTriplet(6000, 11250, 12750),
                        new PythagoreanTriplet(7500, 10000, 12500));
        assertEquals(expected, actual);
    }

}

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?