Created
March 27, 2016 06:23
-
-
Save jinahadam/8e3b7f7ee2c3db6a4b9c 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
require "Prime" | |
def largest_prime_factor(n) | |
return n if n.prime? | |
Prime.each do |p| | |
if n % p == 0 | |
k = n.to_f/p.to_f | |
if k.to_i.prime? | |
return k.to_i | |
else | |
return largest_prime_factor(k.to_i) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment