Skip to content

Instantly share code, notes, and snippets.

@lepht
Created February 7, 2012 16:12
Show Gist options
  • Save lepht/1760465 to your computer and use it in GitHub Desktop.
Save lepht/1760465 to your computer and use it in GitHub Desktop.
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