Skip to content

Instantly share code, notes, and snippets.

@eric1234
Created February 10, 2010 04:23
Show Gist options
  • Select an option

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

Select an option

Save eric1234/300020 to your computer and use it in GitHub Desktop.
Problem 10 on Project Euler
class Numeric
def divisible_by? num
self % num == 0
end
def sqrt
Math.sqrt self
end
def prime?(preprocessed=(2..self.sqrt.floor))
limit = self.sqrt.floor
for test in preprocessed
return true if limit < test
return false if self.divisible_by? test
end
true
end
def primes
ret = []
3.step(self, 2) do |test|
ret << test if test.prime? ret
end
ret.unshift 2
ret
end
end
module Enumerable
def sum
inject(0) {|t, c| t + c}
end
end
puts 1999999.primes.sum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment