Published at May 19 2019
·
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}`

.

For installation and learning resources, refer to the Ruby resources page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

```
gem install minitest
```

If you would like color output, you can `require 'minitest/pride'`

in
the test file, or note the alternative instruction, below, for running
the test file.

Run the tests from the exercise directory using the following command:

```
ruby pythagorean_triplet_test.rb
```

To include color from the command line:

```
ruby -r minitest/pride pythagorean_triplet_test.rb
```

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

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

```
require 'minitest/autorun'
require_relative 'pythagorean_triplet'
class TripletTest < Minitest::Test
def test_sum
assert_equal 12, Triplet.new(3, 4, 5).sum
end
def test_product
skip
assert_equal 60, Triplet.new(3, 4, 5).product
end
def test_pythagorean
skip
assert Triplet.new(3, 4, 5).pythagorean?
end
def test_not_pythagorean
skip
refute Triplet.new(5, 6, 7).pythagorean?
end
def test_triplets_upto_10
skip
triplets = Triplet.where(max_factor: 10)
products = triplets.map(&:product).sort
assert_equal [60, 480], products
end
def test_triplets_from_11_upto_20
skip
triplets = Triplet.where(min_factor: 11, max_factor: 20)
products = triplets.map(&:product).sort
assert_equal [3840], products
end
def test_triplets_where_sum_x
skip
triplets = Triplet.where(sum: 180, max_factor: 100)
products = triplets.map(&:product).sort
assert_equal [118_080, 168_480, 202_500], products
end
def test_where_sum_1000
skip
triplets = Triplet.where(sum: 1_000, min_factor: 200, max_factor: 425)
products = triplets.map(&:product)
assert_equal [31_875_000], products
end
end
```

```
class Triplet
NUM_SIDES = 3
private_constant :NUM_SIDES
def self.where(min_factor: 1, max_factor:, sum: nil)
(min_factor..max_factor)
.to_a
.combination(NUM_SIDES)
.with_object(sum)
.each_with_object([], &method(:add_pythagorean_triplet))
end
def self.add_pythagorean_triplet(((a, b, c), sum), acc)
triplet = Triplet.new(a, b, c)
acc << triplet if pythagorean_triplet?(triplet, sum)
end
private_class_method :add_pythagorean_triplet
def self.pythagorean_triplet?(triplet, sum)
triplet.pythagorean? && (triplet.sum == sum || sum.nil?)
end
private_class_method :pythagorean_triplet?
def initialize(a_num, b_num, c_num)
@sides = [a_num, b_num, c_num].sort
@a, @b, @c = sides
end
def sum
sides.sum
end
def product
sides.reduce(&:*)
end
def pythagorean?
a**2 + b**2 == c**2
end
private
attr_reader :a, :b, :c, :sides
end
```

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?

Level up your programming skills with 3,373 exercises across 50 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments