Created
September 22, 2009 00:58
-
-
Save eric1234/190675 to your computer and use it in GitHub Desktop.
Problem 3 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 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