Skip to content

Instantly share code, notes, and snippets.

@antimon2
Created June 6, 2012 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antimon2/2881178 to your computer and use it in GitHub Desktop.
Save antimon2/2881178 to your computer and use it in GitHub Desktop.
Prime#sexy_pair
require 'prime'
class Prime
def sexy_pair n = nil
return to_enum :sexy_pair, n unless block_given?
each(n).each_cons(3) do |p1, p2, p3|
yield [p1, p2] if p2 - p1 == 6
yield [p1, p3] if p3 - p1 == 6
end
end
def sexy_triplets n = nil
return to_enum :sexy_triplets, n unless block_given?
sexy_hash = {}
sexy_pair(n) do |p1, p2|
sexy_hash[p2] = true
if sexy_hash[p1] && !(p2 + 6).prime?
yield [p1 - 6, p1, p2]
end
end
end
def sexy_quadruplets n = nil
return to_enum :sexy_quadruplets, n unless block_given?
sexy_hash = {}
sexy_pair(n) do |p1, p2|
sexy_hash[p2] = true
if sexy_hash[p1 - 6]
yield [p1 - 12, p1 - 6, p1, p2]
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment