Created
August 15, 2013 18:04
-
-
Save 1dolinski/6243097 to your computer and use it in GitHub Desktop.
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_factors(n) | |
return [] if n == 1 | |
factor = (2..n).find {|x| n % x == 0} | |
binding.pry | |
[factor] + prime_factors(n / factor) | |
binding.pry | |
end | |
puts prime_factors(600851475143).max |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
def prime_factors(n)
return [] if n == 1
factor = (2..n).find {|x| n % x == 0}
[factor] << prime_factors(n / factor) #same as [factor] + prime_factors(n / factor)
end
puts prime_factors(600851475143)
however, puts prime_factors(600851475143).max will break on the << and not on the +