euler/ruby/euler005.rb

20 lines
543 B
Ruby
Raw Normal View History

require_relative 'euler'
def factorization_of_integer_divisible_by_all_integers_up_to(n)
{}.tap do |factorization|
(2..n).map do |i|
2014-12-05 00:50:52 +00:00
Prime.prime_division(i).map do |prime, exponent|
factorization[prime] = exponent if (!factorization.include?(prime) || (exponent > factorization[prime]))
2014-04-25 23:59:14 +00:00
end
end
end
end
def factors_to_int(factorization)
2014-12-05 00:50:52 +00:00
factorization.map { |prime, exponent| prime ** exponent }.inject(:*)
end
2014-04-25 23:59:14 +00:00
def solution
factors_to_int(factorization_of_integer_divisible_by_all_integers_up_to(20))
end