Skip to content

Instantly share code, notes, and snippets.

@masassiez
Created February 15, 2012 14:36
Show Gist options
  • Save masassiez/1836246 to your computer and use it in GitHub Desktop.
Save masassiez/1836246 to your computer and use it in GitHub Desktop.
#42@Rubeque - Prime Factorization
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