Skip to content

Instantly share code, notes, and snippets.

@filipre
Last active February 10, 2017 16:14
Show Gist options
  • Save filipre/840a4a26a2e179eb33234052ad670be3 to your computer and use it in GitHub Desktop.
Save filipre/840a4a26a2e179eb33234052ad670be3 to your computer and use it in GitHub Desktop.
with recursive numbers(n) as (
select n
from generate_series(1,5000) s(n)
),
prim(n, step) as (
select numbers.n, 2
from numbers
union
select prim.n, prim.step + 1
from prim
where prim.step <= (select max(n) from numbers) and (prim.n = prim.step or mod(prim.n, prim.step) != 0)
),
primPrec(n, m) as (
select prim.n, sum(prim.n) over (rows between 1 preceding and 1 preceding)
from prim
where prim.step = (select max(n) from numbers)
),
primTwins(n, m) as (
select n, m
from primPrec
where (n-m) = 2
)
select count(*)
from primTwins
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment