removing unnecessary macros

master
Evan Hemsley 2018-02-24 19:43:30 -08:00
parent 251fb30d67
commit 9da04584e6
3 changed files with 23 additions and 39 deletions

View File

@ -33,47 +33,32 @@ module Euler
end
end
macro prime_factorization_method(given_type)
def prime_factorization(n : {{given_type}})
result = {} of {{given_type}} => 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
macro to_digit_list_method(given_type)
def to_digit_list(n : {{given_type}})
n.to_s.chars.map { |d| d.to_i }
end
end
macro to_big_ints_method(given_type)
def to_big_ints(num_list : Array({{given_type}}))
num_list.map { |n| BigInt.new(n) }
end
end
trial_division_method(NumType)
trial_division_method(BigInt)
prime_factorization_method(NumType)
prime_factorization_method(BigInt)
def prime_factorization(n : NumType | BigInt)
result = {} of (NumType | BigInt) => Int32
factors = self.trial_division(n)
to_digit_list_method(NumType)
to_digit_list_method(BigInt)
factors.each do |f|
result[f] = 0
num = n
while num % f == 0
result[f] += 1
num /= f
end
end
to_big_ints_method(NumType)
to_big_ints_method(BigInt)
result
end
def to_big_ints(num_list : Array(NumType))
num_list.map { |n| BigInt.new(n) }
end
def to_digit_list(n : NumType | BigInt)
n.to_s.chars.map { |d| d.to_i }
end
def palindrome?(x)
x.to_s.reverse == x.to_s

View File

@ -5,7 +5,7 @@ module Euler
extend self
def integer_factorization_divisible_by_all_up_to(n)
result = {} of Euler::NumType => Euler::NumType
result = {} of Euler::NumType | BigInt => Int32
(2..n).map do |i|
Euler.prime_factorization(i).each do |prime, exponent|
if !result.has_key?(prime) || (exponent > result[prime])
@ -16,7 +16,7 @@ module Euler
result
end
def factors_to_int(factorization : Hash(Euler::NumType, Euler::NumType))
def factors_to_int(factorization : Hash(Euler::NumType | BigInt, Int32))
factorization.map { |prime, exponent| prime ** exponent }.product
end

View File

@ -2,7 +2,6 @@ require "./euler"
module Euler
class Prime
# needs to include BigInt ...
include Iterator(NumType | BigInt)
def initialize()