euler/crystal/euler005.cr

28 lines
692 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 Problem005
extend self
def integer_factorization_divisible_by_all_up_to(n)
2018-02-25 03:43:30 +00:00
result = {} of Euler::NumType | BigInt => Int32
2018-02-25 03:32:39 +00:00
(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
2018-02-23 04:53:01 +00:00
end
2018-02-25 03:32:39 +00:00
result
2018-02-23 04:53:01 +00:00
end
2018-02-25 03:43:30 +00:00
def factors_to_int(factorization : Hash(Euler::NumType | BigInt, Int32))
2018-02-25 03:32:39 +00:00
factorization.map { |prime, exponent| prime ** exponent }.product
end
2018-02-23 04:53:01 +00:00
2018-02-25 03:32:39 +00:00
def solution
factors_to_int(integer_factorization_divisible_by_all_up_to(20))
end
end
end