euler/crystal/euler005.cr

20 lines
550 B
Crystal
Raw Normal View History

2018-02-23 04:53:01 +00:00
require "./euler"
def integer_factorization_divisible_by_all_up_to(n)
result = {} of Euler::NumType => Euler::NumType
(2..n).map do |i|
Euler.prime_factorization(i).each do |prime, exponent|
if !result.has_key?(prime) || (exponent > result[prime])
result[prime] = exponent
end
end
end
result
end
def factors_to_int(factorization : Hash(Euler::NumType, Euler::NumType))
factorization.map { |prime, exponent| prime ** exponent }.product
end
puts factors_to_int(integer_factorization_divisible_by_all_up_to(20))