Skip to content

Instantly share code, notes, and snippets.

@eric1234
Created September 22, 2009 00:58
Show Gist options
  • Select an option

  • Save eric1234/190675 to your computer and use it in GitHub Desktop.

Select an option

Save eric1234/190675 to your computer and use it in GitHub Desktop.
Problem 3 on Project Euler
class Numeric
def divisible_by? num
self % num == 0
end
def prime?
not (2..self.sqrt.floor).any? {|i| self.divisible_by? i}
end
def sqrt
Math.sqrt self
end
def primes
ret = []
self.sqrt.ceil.step(1, -2) do |test|
if self.divisible_by? test
multiple = self / test
ret << multiple if multiple.prime?
ret << test if test.prime?
end
end
ret
end
end
puts 600851475143.primes.max
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment