summaryrefslogtreecommitdiff
path: root/spec/euler/problem_nine_spec.rb
blob: 7b6cdb478029447bf6b38a18adfd10dd798b6150 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
require "spec_helper"

describe "problem nine" do
  # 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 abc.

  # a < b < c
  # c^2 = a^2 + b^2
  class PythagoreanTriplet
    include Enumerable
    attr_reader :max

    def initialize(max = 1_000)
      @max = max
    end

    def each(&block)
      triplets.each(&block)
    end

    private

    def triplets
      Enumerator.new do |yielder|
        (2..max).each do |m|
          (1...m).each do |n|
            # https://en.wikipedia.org/wiki/Pythagorean_triple#Proof_of_Euclid.27s_formula
            a, b, c = m*m - n*n, 2*m*n, m*m + n*n
            yielder.yield([a, b, c])
          end
        end
      end
    end
  end

  subject { PythagoreanTriplet.new }

  it "returns the triplet for 25" do
    expect(subject.first).to eql([3, 4, 5])

    result = subject.find do |triplet|
      (triplet.last * triplet.last) == 25
    end
    expect(result).to eql([3, 4, 5])
    expect(subject).to include([3, 4, 5])
    expect(subject).to include([5, 12, 13])
  end

  it "returns the triplet for 1000" do
    result = subject.find do |triplet|
      (triplet[0] + triplet[1] + triplet[2]) == 1_000
    end
    expect(result).to eql([375, 200, 425])
    puts 375*200*425
  end
end