diff --git a/crystal/euler.cr b/crystal/euler.cr index 0bf385d..9bad941 100644 --- a/crystal/euler.cr +++ b/crystal/euler.cr @@ -7,51 +7,6 @@ module Euler alias NumType = Int32 | Int64 | UInt32 | UInt64 - macro trial_division_method(given_type) - def trial_division(n : {{given_type}}) - factors = [] of {{given_type}} - check = ->(p: {{given_type}}) { - q, r = n.divmod(p) - while r.zero? - factors << p - n = q - q, r = n.divmod(p) - end - } - - check.call(2) - check.call(3) - p = 5 - while p * p <= n - check.call(p) - p += 2 - check.call(p) - p += 4 - end - factors << n if n > 1 - factors - end - end - - trial_division_method(NumType) - trial_division_method(BigInt) - - def prime_factorization(n : NumType | BigInt) - result = {} of (NumType | BigInt) => Int32 - factors = self.trial_division(n) - - factors.each do |f| - result[f] = 0 - num = n - while num % f == 0 - result[f] += 1 - num /= f - end - end - - result - end - def to_big_ints(num_list : Array(NumType)) num_list.map { |n| BigInt.new(n) } end diff --git a/crystal/euler003.cr b/crystal/euler003.cr index 5a86c82..d9075d2 100644 --- a/crystal/euler003.cr +++ b/crystal/euler003.cr @@ -5,7 +5,7 @@ module Euler extend self def solution - Euler.trial_division(600851475143).max + Euler::Prime.trial_division(600851475143).max end end end diff --git a/crystal/euler005.cr b/crystal/euler005.cr index eebc5e6..4199421 100644 --- a/crystal/euler005.cr +++ b/crystal/euler005.cr @@ -7,7 +7,7 @@ module Euler def integer_factorization_divisible_by_all_up_to(n) result = {} of Euler::NumType | BigInt => Int32 (2..n).map do |i| - Euler.prime_factorization(i).each do |prime, exponent| + Euler::Prime.prime_factorization(i).each do |prime, exponent| if !result.has_key?(prime) || (exponent > result[prime]) result[prime] = exponent end diff --git a/crystal/prime.cr b/crystal/prime.cr index 99514ad..fba4c93 100644 --- a/crystal/prime.cr +++ b/crystal/prime.cr @@ -64,5 +64,50 @@ module Euler result = sieve.map_with_index { |b, i| b ? i : nil }.compact end + + macro trial_division_method(given_type) + def self.trial_division(n : {{given_type}}) + factors = [] of {{given_type}} + check = ->(p: {{given_type}}) { + q, r = n.divmod(p) + while r.zero? + factors << p + n = q + q, r = n.divmod(p) + end + } + + check.call(2) + check.call(3) + p = 5 + while p * p <= n + check.call(p) + p += 2 + check.call(p) + p += 4 + end + factors << n if n > 1 + factors + end + end + + trial_division_method(NumType) + trial_division_method(BigInt) + + def self.prime_factorization(n : NumType | BigInt) + result = {} of (NumType | BigInt) => Int32 + factors = self.trial_division(n) + + factors.each do |f| + result[f] = 0 + num = n + while num % f == 0 + result[f] += 1 + num /= f + end + end + + result + end end end