Last active
February 10, 2017 16:14
-
-
Save filipre/840a4a26a2e179eb33234052ad670be3 to your computer and use it in GitHub Desktop.
This file contains 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
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