Skip to content

Instantly share code, notes, and snippets.

@Veedrac
Created June 25, 2015 07:59
Show Gist options
  • Save Veedrac/1e95a577078410d32de5 to your computer and use it in GitHub Desktop.
Save Veedrac/1e95a577078410d32de5 to your computer and use it in GitHub Desktop.
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}."
@Veedrac
Copy link
Author

Veedrac commented Jun 25, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment