From 9da04584e6c2ebfa676b1dbca2fb1c78e60bd099 Mon Sep 17 00:00:00 2001 From: Evan Hemsley Date: Sat, 24 Feb 2018 19:43:30 -0800 Subject: [PATCH] removing unnecessary macros --- crystal/euler.cr | 57 +++++++++++++++++---------------------------- crystal/euler005.cr | 4 ++-- crystal/prime.cr | 1 - 3 files changed, 23 insertions(+), 39 deletions(-) diff --git a/crystal/euler.cr b/crystal/euler.cr index 2c3f05e..0bf385d 100644 --- a/crystal/euler.cr +++ b/crystal/euler.cr @@ -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 diff --git a/crystal/euler005.cr b/crystal/euler005.cr index 18566b8..eebc5e6 100644 --- a/crystal/euler005.cr +++ b/crystal/euler005.cr @@ -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 diff --git a/crystal/prime.cr b/crystal/prime.cr index 02a5612..99514ad 100644 --- a/crystal/prime.cr +++ b/crystal/prime.cr @@ -2,7 +2,6 @@ require "./euler" module Euler class Prime - # needs to include BigInt ... include Iterator(NumType | BigInt) def initialize()