-
Notifications
You must be signed in to change notification settings - Fork 0
/
solve9.rb
61 lines (51 loc) · 1.09 KB
/
solve9.rb
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
60
61
# https://projecteuler.net/problem=9
# Run with: 'ruby solve9.rb'
# using Ruby 2.5.1
# by Zack Sargent
# Prompt:
# 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.
# This uses the primes generation formula from:
# https://www.mathsisfun.com/numbers/pythagorean-triples.html
SUM = 1_000
class Triples
def initialize(m, n)
raise "invalid inital value" unless m > n
@a = m ** 2 - n ** 2
@b = 2 * m * n
@c = m ** 2 + n ** 2
end
def sum
return @a + @b + @c
end
def valid?
if @a ** 2 + @b ** 2 == @c ** 2
if self.sum == SUM
return true
end
end
return false
end
def solve
return @a * @b * @c
end
end
# theoretically O(n^2),
# but in practice it returns early.
(2..SUM).each do |m|
(1..SUM).each do |n|
break if n >= m
triples = Triples.new(m, n)
if triples.valid?
p triples.solve
return
end
end
end
# -> 31875000