Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of rootulp

rootulp's solution

to Pythagorean Triplet in the Ruby Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

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.


For installation and learning resources, refer to the exercism help 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

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.

pythagorean_triplet_test.rb

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
end
# Pythagorean Theorem
# a**2 + b**2 = c**2

# Euclid's Formula
# (m**2 - n**2) + (2mn) = (m**2 + n**2)

require 'set'

# Triplet
class Triplet
  attr_reader :a, :b, :c, :sides
  def initialize(a, b, c)
    @a = a
    @b = b
    @c = c
    @sides = Set.new [a, b, c]
  end

  def sum
    sides.reduce(:+)
  end

  def product
    sides.reduce(:*)
  end

  def pythagorean?
    a**2 + b**2 == c**2
  end

  def self.where(constraints = {})
    min_factor = constraints.fetch(:min_factor, 1)
    max_factor = constraints.fetch(:max_factor)
    sum = constraints.fetch(:sum, nil)

    find_triplets(min_factor, max_factor, sum)
  end

  def self.find_triplets(min_factor, max_factor, sum)
    triplets = []
    (min_factor..max_factor).to_a.combination(3).each do |a, b, c|
      triplets << Triplet.new(a, b, c) if valid?(Triplet.new(a, b, c), sum)
    end
    triplets
  end

  def self.valid?(triplet, sum)
    valid_sum?(triplet, sum) && valid_pythagorean?(triplet)
  end

  def self.valid_sum?(triplet, sum)
    return true if sum.nil?
    sum == triplet.sum
  end

  def self.valid_pythagorean?(triplet)
    triplet.pythagorean?
  end
end

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?