27 lines
565 B
Crystal
27 lines
565 B
Crystal
|
#wrong approach, should just do recursive trial division...
|
||
|
|
||
|
def eratosthenes_sieve(n)
|
||
|
sieve = Array.new(n + 1, true)
|
||
|
sieve[0] = false
|
||
|
sieve[1] = false
|
||
|
|
||
|
2.step(to: Math.sqrt(n)) do |i|
|
||
|
if sieve[i]
|
||
|
(i * i).step(to: n, by: i) do |j|
|
||
|
sieve[j] = false
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
result = sieve.map_with_index { |b, i| b ? i : nil }.compact
|
||
|
end
|
||
|
|
||
|
def prime_factors(n)
|
||
|
nums = eratosthenes_sieve(n).select { |i| n % i == 0 }
|
||
|
nums = nums + nums.map { |i| n / i }
|
||
|
end
|
||
|
|
||
|
puts eratosthenes_sieve(600851475143)
|
||
|
|
||
|
puts prime_factors(600851475143).max
|