Created
June 25, 2015 08:06
-
-
Save Aiishe/e82c2681317d41becbeb to your computer and use it in GitHub Desktop.
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
# investigate: #lazy #select | |
require 'prime' | |
class Integer | |
def factors # returns an array of all factors of self | |
(1..Math.sqrt(self) + 1).lazy.select { |n| (self % n).zero? } | |
end | |
end | |
puts "Started at #{Time.now}." | |
max = 100_000_000 | |
counter = 0 | |
primes = Array.new(max+1) { |i| false } | |
Prime.each do |prime| | |
break if prime > max | |
primes[prime] = true | |
n = prime = 1 | |
factors = n.factors | |
counter += n if factors.all? { |d| primes[d + n/d] } | |
end | |
p counter | |
puts "Ended at #{Time.now}." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://codereview.stackexchange.com/questions/94577/euler-357-solution-efficiency/94629?noredirect=1#comment172543_94629