Created
June 6, 2012 10:26
-
-
Save antimon2/2881178 to your computer and use it in GitHub Desktop.
Prime#sexy_pair
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 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