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,9 +33,11 @@ module Euler
end
end
macro prime_factorization_method(given_type)
def prime_factorization(n : {{given_type}})
result = {} of {{given_type}} => Int32
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|
@ -49,32 +51,15 @@ module Euler
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}}))
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
trial_division_method(NumType)
trial_division_method(BigInt)
prime_factorization_method(NumType)
prime_factorization_method(BigInt)
to_digit_list_method(NumType)
to_digit_list_method(BigInt)
to_big_ints_method(NumType)
to_big_ints_method(BigInt)
def palindrome?(x)
x.to_s.reverse == x.to_s
end

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()