Skip to content

Instantly share code, notes, and snippets.

@jtallant
Created July 2, 2012 01:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtallant/3030295 to your computer and use it in GitHub Desktop.
Save jtallant/3030295 to your computer and use it in GitHub Desktop.
Project Euler Problem 3 Ruby
# 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