-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler0009.rb
38 lines (32 loc) · 854 Bytes
/
euler0009.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
require_relative "lib/helpers"
show_problem_info({
number: 9,
title: "Special Pythagorean triplet",
statement: %Q{
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.
Assume c >= b >= a
},
url: "https://projecteuler.net/problem=9"
})
LIMIT = 1000
benchmark_solution(31875000) do
answer = 0
(LIMIT - 2).downto((LIMIT/3.0).ceil) do |c|
(1000 - c - 1).downto((c/2).ceil) do |b|
a = 1000 - c - b
if a ** 2 + b ** 2 == c ** 2
p "a = #{a}, b = #{b}, c = #{c}"
p "#{c ** 2}"
answer = a * b * c
break
end
break if answer > 0
end
break if answer > 0
end
answer
end