Created
July 2, 2012 01:00
-
-
Save jtallant/3030295 to your computer and use it in GitHub Desktop.
Project Euler Problem 3 Ruby
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
# if n is 1, end the method and return empty array | |
# set var factor = first number in range 2..n | |
# that has a remainder of 0 when divided by x | |
# x being the current iteration of 2..n | |
# put factor var into an array and then call the | |
# method we are currently in again passing the | |
# value of n divided by the current value of factor | |
def prime_factors(n) | |
return [] if n == 1 | |
factor = (2..n).find {|x| n % x == 0} | |
[factor] + prime_factors(n / factor) | |
end | |
puts prime_factors(600851475143).max |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment