committing old stuff idk

master
Evan Hemsley 2018-08-16 17:04:11 -07:00
parent 9da04584e6
commit 6d98dbb8b6
4 changed files with 47 additions and 47 deletions

View File

@ -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

View File

@ -5,7 +5,7 @@ module Euler
extend self
def solution
Euler.trial_division(600851475143).max
Euler::Prime.trial_division(600851475143).max
end
end
end

View File

@ -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

View File

@ -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