-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem39.rb
51 lines (42 loc) · 945 Bytes
/
Problem39.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
# If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.
#
# {20,48,52}, {24,45,51}, {30,40,50}
#
# For which value of p <= 1000, is the number of solutions maximised?
#
beginning_time = Time.now
class Integer
def num_solutions
solutions = []
a,b,c = 1,1,0
while a < self
while b < self
c = self-a-b
if a**2 + b**2 == c**2
solutions << [a,b,c]
end
b+=1
end
b=1
a+=1
end
return solutions.map { |x| x.sort}.uniq.length
end
end
solutions = 0
maxP = 0
maxSolutions = 0
1.upto(1000) do |p|
if p%1 == 0
solutions = p.num_solutions
if solutions > maxSolutions
maxSolutions = solutions
maxP = p
puts maxP.to_s + " " + maxSolutions.to_s
end
end
end
p maxP
end_time = Time.now
puts
puts "Time elapsed #{(end_time - beginning_time)} seconds"