-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9.rb
50 lines (40 loc) · 846 Bytes
/
9.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
require_relative 'lib/helper'
# A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
# a^2 + b^2 = c^2
# For example, 32 + 42 = 9 + 16 = 25 = 52.
# There exists exactly one Pythagorean triplet for which a + b + c = 1000.
# Find the product abc.
def squares
sqs = []
c = 1
while (v = c ** 2) < 1000000
sqs << v
c += 1
end
sqs
end
def simple
squares.combination(3).each do |c|
if c[0] + c[1] == c[2]
sqrts = c.map { |c| Math.sqrt(c) }
if sqrts.inject(&:+) == 1000
return sqrts.inject(&:*)
end
end
end
end
def triplet?(a, b, c)
return true if (c ** 2 == (a ** 2 + b ** 2))
false
end
def better
(1..1000).each do |a|
(a..1000).each do |b|
c = (1000 - a - b)
return (a * b * c) if triplet?(a, b, c)
end
end
end
run do
better
end