Skip to content

Instantly share code, notes, and snippets.

@Aiishe
Created June 25, 2015 08:06
Show Gist options
  • Save Aiishe/e82c2681317d41becbeb to your computer and use it in GitHub Desktop.
Save Aiishe/e82c2681317d41becbeb to your computer and use it in GitHub Desktop.
# 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