Created
September 12, 2017 17:25
-
-
Save KevinSia/82fba690b4b05e6c380853de1956c6d0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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_factors(num) | |
return [num] if num <= 1 | |
result = [] | |
until num == 1 | |
for i in (2..num) | |
if num % i == 0 | |
result << i | |
num /= i | |
break | |
end | |
end | |
end | |
return result | |
end |
This file contains hidden or 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_factors(num) | |
return [num] if num <= 1 | |
result = [] | |
i = 2 | |
until num == 1 | |
if num % i == 0 | |
result << i | |
num /= i | |
i = 2 | |
else | |
i += 1 | |
end | |
end | |
result | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment