removing unnecessary macros
							parent
							
								
									251fb30d67
								
							
						
					
					
						commit
						9da04584e6
					
				|  | @ -33,47 +33,32 @@ module Euler | ||||||
|     end |     end | ||||||
|   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(NumType) | ||||||
|   trial_division_method(BigInt) |   trial_division_method(BigInt) | ||||||
| 
 | 
 | ||||||
|   prime_factorization_method(NumType) |   def prime_factorization(n : NumType | BigInt) | ||||||
|   prime_factorization_method(BigInt) |     result = {} of (NumType | BigInt) => Int32 | ||||||
|  |     factors = self.trial_division(n) | ||||||
| 
 | 
 | ||||||
|   to_digit_list_method(NumType) |     factors.each do |f| | ||||||
|   to_digit_list_method(BigInt) |       result[f] = 0 | ||||||
|  |       num = n | ||||||
|  |       while num % f == 0 | ||||||
|  |         result[f] += 1 | ||||||
|  |         num /= f | ||||||
|  |       end | ||||||
|  |     end | ||||||
| 
 | 
 | ||||||
|   to_big_ints_method(NumType) |     result | ||||||
|   to_big_ints_method(BigInt) |   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) |   def palindrome?(x) | ||||||
|     x.to_s.reverse == x.to_s |     x.to_s.reverse == x.to_s | ||||||
|  |  | ||||||
|  | @ -5,7 +5,7 @@ module Euler | ||||||
|     extend self |     extend self | ||||||
| 
 | 
 | ||||||
|     def integer_factorization_divisible_by_all_up_to(n) |     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| |       (2..n).map do |i| | ||||||
|         Euler.prime_factorization(i).each do |prime, exponent| |         Euler.prime_factorization(i).each do |prime, exponent| | ||||||
|           if !result.has_key?(prime) || (exponent > result[prime]) |           if !result.has_key?(prime) || (exponent > result[prime]) | ||||||
|  | @ -16,7 +16,7 @@ module Euler | ||||||
|       result |       result | ||||||
|     end |     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 |       factorization.map { |prime, exponent| prime ** exponent }.product | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -2,7 +2,6 @@ require "./euler" | ||||||
| 
 | 
 | ||||||
| module Euler | module Euler | ||||||
|   class Prime |   class Prime | ||||||
|     # needs to include BigInt ... |  | ||||||
|     include Iterator(NumType | BigInt) |     include Iterator(NumType | BigInt) | ||||||
| 
 | 
 | ||||||
|     def initialize() |     def initialize() | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue