Created
February 10, 2010 04:23
-
-
Save eric1234/300020 to your computer and use it in GitHub Desktop.
Problem 10 on Project Euler
This file contains hidden or 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 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