euler/euler005.rb

25 lines
630 B
Ruby
Raw Normal View History

require_relative 'euler'
require 'prime'
def factorization_of_integer_divisible_by_all_integers_up_to(n)
{}.tap do |factorization|
(2..n).map do |i|
Prime.prime_division(i).map do |factor|
factorization[factor.first] = factor.last if (!factorization.include?(factor.first) || (factor.last > factorization[factor.first]))
2014-04-25 23:59:14 +00:00
end
end
end
end
def factors_to_int(factorization)
[].tap do |result|
factorization.each do |prime, exponent|
result << prime ** exponent
end
end.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