euler/crystal/euler009.cr

23 lines
500 B
Crystal
Raw Normal View History

2018-02-23 04:53:01 +00:00
require "./euler"
2018-02-25 03:32:39 +00:00
module Euler
module Problem009
extend self
def generate_pythagorean_triples(upper_bound)
([] of Array(Euler::NumType)).tap do |triples|
(2..upper_bound).each do |a|
(a..upper_bound).each do |b|
c = Math.sqrt(a**2 + b**2)
triples << [a, b, c.to_i] if c % 1 == 0
end
end
2018-02-23 04:53:01 +00:00
end
end
2018-02-25 03:32:39 +00:00
def solution
generate_pythagorean_triples(500).find([-1]) { |x| x.sum == 1000 }.product
end
2018-02-23 04:53:01 +00:00
end
end