Created
February 15, 2012 14:36
-
-
Save masassiez/1836246 to your computer and use it in GitHub Desktop.
#42@Rubeque - Prime Factorization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def prime_factorization(num) | |
prime = Enumerator.new do |y| | |
mem = [] | |
is_prime = lambda { |e| mem.find { |_| e.gcd(_) != 1 } } | |
2.upto(num) do |n| | |
unless is_prime[n] | |
mem << n | |
y << n | |
end | |
end | |
end | |
o = [] | |
loop do | |
n = prime.next | |
if num % n == 0 | |
num /= n | |
o << n | |
prime.rewind | |
end | |
end | |
o | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment