Skip to content

Instantly share code, notes, and snippets.

@antimon2
Created June 6, 2012 10:45
Show Gist options
  • Select an option

  • Save antimon2/2881244 to your computer and use it in GitHub Desktop.

Select an option

Save antimon2/2881244 to your computer and use it in GitHub Desktop.
Prime#cosins
require 'prime'
class Prime
def cosins n = nil
return to_enum :cosins, n unless block_given?
yield [3, 7]
each(n).each_cons(2) do |p1, p2|
yield [p1, p2] if p2 - p1 == 4
end
end
end
# Prime.cosins.take(10)
# => [[3, 7], [7, 11], [13, 17], [19, 23], [37, 41], [43, 47], [67, 71], [79, 83], [97, 101], [103, 107]]
# Prime.cosins(100).to_a
# => [[3, 7], [7, 11], [13, 17], [19, 23], [37, 41], [43, 47], [67, 71], [79, 83]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment