-
-
Save Veedrac/1e95a577078410d32de5 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
require 'prime' | |
class Integer | |
# Make a lazy enumerator of the lower half of factors of self | |
def half_factors | |
(1..Math.sqrt(self) + 1).lazy.select { |n| (self % n).zero? } | |
end | |
end | |
puts "Started at #{Time.now}." | |
counter = 0 | |
max = 100_000_000 | |
primes = Array.new(max+1) { |i| false } | |
Prime.each do |prime| | |
break if prime > max | |
primes[prime] = true | |
n = prime - 1 | |
half_factors = n.half_factors | |
counter += n if half_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/