Published at Feb 18 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
attr_reader :a
attr_reader :b
attr_reader :c
def self.where(min_factor: 1, max_factor:, sum: nil)
triplets = []
(min_factor...max_factor - 2).each do |a|
(a + 1..max_factor - 1).each do |b|
c = Math.sqrt(a**2 + b**2).to_i
triplet = Triplet.new(a, b, c)
if triplet.pythagorean?
if sum.nil? || triplet.sum == sum
triplets << triplet
end
end
end
end
triplets
end
def initialize(a, b, c)
@a, @b, @c = a, b, c
end
def sum
a + b + c
end
def product
a * b * c
end
def pythagorean?
a**2 + b**2 == c**2
end
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,279 exercises across 51 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments