Created
July 13, 2013 16:37
-
-
Save shirakia/5991270 to your computer and use it in GitHub Desktop.
simple 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 factorize(num) | |
ans = Hash.new(0) | |
while true | |
prime_factor = find_prime_factor(num) | |
break if 0 == prime_factor | |
ans[prime_factor] += 1 | |
num /= prime_factor | |
end | |
ans | |
end | |
def find_prime_factor(num) | |
(2..num).each do |i| | |
return i if 0 == num % i | |
end | |
0 | |
end | |
p factorize(ARGV.first.to_i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment