Created
February 7, 2012 16:12
-
-
Save lepht/1760465 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
class Integer | |
def prime? | |
if self == 1 | |
return false | |
else | |
(2...self).each { |n| | |
return false if self % n == 0 | |
} | |
return true | |
end | |
end | |
def factor?(divisor) | |
return self % divisor == 0 | |
end | |
def largest_prime_factor | |
(2..Math.sqrt(self)).reverse_each { |n| | |
if self.factor?(n) && n.prime? | |
return n | |
end | |
} | |
end | |
end | |
puts 600_851_475_143.largest_prime_factor |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment