Twin Prime Calculator: Pairs That Keep Appearing No Matter How High You Look
About the Author

Twin primes are two primes that differ by exactly 2, such as 11 and 13 or 101 and 103. There are 8 such pairs below 100, 35 below 1,000 and 8,169 below one million, and they never seem to stop: the largest pair anyone has found has 388,342 digits. This twin prime calculator lists every pair inside any range up to 10¹², counts the cousin pairs (gap 4) and sexy pairs (gap 6) in the same sweep, sets the count against the Hardy–Littlewood prediction, and can also jump straight to the first pairs after a number such as a billion.
Two conventions matter when you compare with a textbook. A pair only counts when both primes sit inside the range, so “cousin primes up to 100” gives 8 pairs and leaves out (97, 101). And the pair (2, 3) is not a twin pair, even though those primes are neighbours: twin means a difference of 2, and the only two primes 2 apart that share the number 5 are (3, 5) and (5, 7).
The Twin Primes List Up to 1,000 and the Pattern Inside It
Here are all 35 pairs with both members below 1,000, exactly as the calculator lists them:
(3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43), (59, 61), (71, 73), (101, 103), (107, 109), (137, 139), (149, 151), (179, 181), (191, 193), (197, 199), (227, 229), (239, 241), (269, 271), (281, 283), (311, 313), (347, 349), (419, 421), (431, 433), (461, 463), (521, 523), (569, 571), (599, 601), (617, 619), (641, 643), (659, 661), (809, 811), (821, 823), (827, 829), (857, 859), (881, 883)
Look at the number sitting between each pair: 6, 12, 18, 30, 42, 60, 72, 102, 108 and so on. From (5, 7) onward it is always a multiple of 6. The reason is short. Among any three consecutive whole numbers p, p + 1, p + 2, one is divisible by 3; if p and p + 2 are both primes above 3, the multiple of 3 must be the middle one, and it is also even because it sits between two odd numbers. So every twin pair above (3, 5) has the form (6n − 1, 6n + 1), which is why the calculator’s sieve only ever finds pairs straddling a multiple of 6. The same three-in-a-row argument shows why 5 is the only prime that belongs to two twin pairs: for (p, p + 2, p + 4) to be all prime, one of them must be 3 itself.
The list also shows how many primes are not twins. Below 100 there are 25 primes but only 15 of them belong to a twin pair; the other ten, namely 2, 23, 37, 47, 53, 67, 79, 83, 89 and 97, are called isolated primes. As the numbers grow the isolated primes take over. The table below is the calculator’s own output for ranges starting at 0; the prime counts match the nth prime calculator, which counts single primes rather than pairs.
| Up to | Primes | Twin pairs | Primes per twin pair |
|---|---|---|---|
| 100 | 25 | 8 | 3.1 |
| 1,000 | 168 | 35 | 4.8 |
| 10,000 | 1,229 | 205 | 6.0 |
| 100,000 | 9,592 | 1,224 | 7.8 |
| 1,000,000 | 78,498 | 8,169 | 9.6 |
| 10,000,000 | 664,579 | 58,980 | 11.3 |
How the Twin Prime Calculator Finds Pairs in a Range Without Testing Every Number
Testing each number for primality one at a time, the way a prime calculator does for a single value, is the wrong tool for a range: a million-wide window would mean a million separate tests. A segmented sieve does the whole window at once. Take the range 1,000 to 1,100. Any composite number up to 1,100 has a prime factor no larger than √1,100 ≈ 33, so the only primes needed are the 11 primes from 2 to 31. Cross out every multiple of each of them inside the window, and the 16 survivors are the primes between 1,000 and 1,100:
1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097
Now read the pairs off the list. Neighbours that differ by 2 are (1019, 1021), (1031, 1033), (1049, 1051), (1061, 1063) and (1091, 1093): five twin pairs. Primes 4 apart give the cousin pairs (1009, 1013), (1087, 1091) and (1093, 1097), and primes 6 apart give five sexy pairs, (1013, 1019), (1033, 1039), (1063, 1069), (1087, 1093) and (1091, 1097). Notice that 1091 and 1093 belong to three different families at once, and that 1093 is famous in its own right as the smaller of the two known Wieferich primes.
Two details make this fast enough for a ten-million-wide window in well under a second. First, the base primes stop at the square root of the top of the range, so even at 10¹² only the 78,498 primes below one million are needed. Second, the first multiple of each base prime inside the window is found with a single remainder calculation rather than by walking up from zero, so a window near 10¹² costs the same as one near 0. Crossing out is the whole cost: a number that survives is prime because nothing divided it, so no primality test of any kind runs afterwards. For the factors of the numbers that were crossed out, the prime factorization calculator shows the full breakdown.
Cousin Primes and Sexy Primes: The Same Search With a Different Gap
Cousin primes differ by 4 and sexy primes by 6, the name being a pun on sex, the Latin word for six. The first cousin pairs are (3, 7), (7, 11), (13, 17) and (19, 23); the first sexy pairs are (5, 11), (7, 13), (11, 17) and (13, 19). Because the calculator reads all three gaps from the same sieve, switching the pair type never repeats the crossing-out, and the three counts appear side by side whichever list is open.
| Up to | Twin (gap 2) | Cousin (gap 4) | Sexy (gap 6) | Sexy ÷ twin |
|---|---|---|---|---|
| 100 | 8 | 8 | 15 | 1.88 |
| 1,000 | 35 | 41 | 74 | 2.11 |
| 10,000 | 205 | 203 | 411 | 2.00 |
| 100,000 | 1,224 | 1,216 | 2,447 | 2.00 |
| 1,000,000 | 8,169 | 8,144 | 16,386 | 2.01 |
| 10,000,000 | 58,980 | 58,622 | 117,207 | 1.99 |
Twin and cousin pairs run neck and neck, while sexy pairs are almost exactly twice as common, and the reason is again division by 3. A prime above 3 leaves remainder 1 or 2 when divided by 3. For a twin pair, p and p + 2 must avoid remainder 0, which forces p to have remainder 2; the same squeeze applies to cousins, where p + 4 has the same remainder as p + 1. A gap of 6 changes nothing modulo 3, so p may have either remainder, and twice as many candidates survive. That factor of 2 is built into the Hardy–Littlewood estimate the calculator shows for sexy pairs.
The gap-6 family also produces longer chains than the others. Because 3 divides one of any three numbers spaced 4 apart, (3, 7, 11) is the only cousin triple, and 7 is the only prime in two cousin pairs. Sexy primes can form triplets like (7, 13, 19), quadruplets like (5, 11, 17, 23), and a single quintuplet, (5, 11, 17, 23, 29), which cannot be beaten because 5 divides one of any five numbers spaced 6 apart. Load the “Sexy primes to 100” example and the quadruplet appears as the three overlapping pairs (5, 11), (11, 17) and (17, 23).
How Many Twin Primes Are There? The Hardy–Littlewood Prediction
Nobody has proved that twin primes go on forever. What is known is a bounded-gap result: Yitang Zhang showed in 2013 that some fixed gap below 70 million occurs infinitely often, and the Polymath project brought that gap down to 246 the following year. From 246 to 2 is the distance that remains. What mathematicians do have is a remarkably accurate prediction of how many pairs there should be, the first Hardy–Littlewood conjecture:
π₂(x) ≈ 2C₂ ∫₂ˣ dt / (ln t)² C₂ = 0.6601618158…
C₂ is the twin prime constant, the product over odd primes of 1 − 1/(p − 1)². The calculator evaluates the integral for whatever range you enter, so the estimate works for a window that starts at ten billion just as well as for one that starts at zero. Against the counts from the sieve, the estimate lands within 3% from 10,000 upward:
| Range | Found | Estimate | Found ÷ estimate |
|---|---|---|---|
| 0 to 1,000 | 35 | 45.8 | 0.76 |
| 0 to 10,000 | 205 | 214.2 | 0.96 |
| 0 to 100,000 | 1,224 | 1,248.7 | 0.98 |
| 0 to 1,000,000 | 8,169 | 8,248.0 | 0.99 |
| 0 to 10,000,000 | 58,980 | 58,753.8 | 1.00 |
| 1,000,000 to 2,000,000 | 6,702 | 6,550.1 | 1.02 |
| 10¹² − 10⁶ to 10¹² | 1,778 | 1,729.4 | 1.03 |
The last row is the “A million below 10¹²” example. That window holds 36,400 primes, and 1,778 of them start a twin pair, roughly one prime in twenty. The estimate is simply the density 2C₂/(ln x)² times the width: at 10¹², (ln x)² ≈ 763, so about 1.32/763 ≈ 0.0017 of all numbers, or 1,729 per million, begin a twin pair. The same density explains the bar chart under the calculator. In the first million, the tenths run 1,224, 936, 834, 810, 761, 766, 730, 705, 706, 697 pairs while the estimate runs 1,249, 933, 856, 811, 780, 756, 737, 722, 708, 697: the pairs thin out exactly as 1/(ln x)² says they should, slowly enough that even at 10¹² a million-wide window is still full of them.
Thinning out has a famous consequence. Viggo Brun proved in 1919 that the sum 1/3 + 1/5 + 1/5 + 1/7 + 1/11 + 1/13 + … over all twin primes converges, even though the corresponding sum over all primes does not. The limit, Brun’s constant, is about 1.902160583, and it is approached agonisingly slowly: the pairs below one million add up to 1.7108, the pairs below ten million to 1.7384, and every pair below 10¹⁶ still only reaches 1.8305. The published value comes from extrapolating that tail with the Hardy–Littlewood density, not from adding it up.
Droughts, Record Pairs and Searching Near Large Numbers
Averages hide the local swings, which is why the calculator reports the longest stretch without a pair. Below one million the record drought runs from the pair (850,349, 850,351) to (851,801, 851,803), 1,452 numbers apart with no twin pair between them, against an average spacing near there of about 150. Below ten million the record grows only to 1,722, and in the million-wide window under 10¹² it is 4,500, roughly eight times the average spacing of 580 in that region. The droughts grow, but far more slowly than the numbers do.
For a single large starting point, the next-pair mode is the better tool. The first twin pair after one billion is (1,000,000,007, 1,000,000,009), after 2³² it is (4,294,967,387, 4,294,967,389), and after 10¹² it is (1,000,000,000,061, 1,000,000,000,063), only 61 above the trillion. Beyond 10¹² the sieve would need base primes above a million and the method stops being instant, so the calculator stops there. Record-sized twin primes are found differently, by testing candidates of a special form; the current record, 2,996,863,034,895 × 2¹²⁹⁰⁰⁰⁰ ± 1 with 388,342 digits, was found in 2016 using the same kind of specialised primality proofs that the Mersenne prime calculator describes for numbers of the form 2ᵖ − 1.
Mistakes That Change the Count
- Counting a pair that leans out of the range. “Sexy primes below 100” is 15 pairs, not 16, because (97, 103) reaches past 100. If you want pairs by their smaller member, extend the end of the range by the gap.
- Treating (2, 3) as twin primes. They are consecutive primes, but consecutive is not the definition. Twin primes differ by 2, and 2 is the only even prime, so the first twin pair is (3, 5).
- Expecting the Hardy–Littlewood estimate to be exact. It is an asymptotic density. Below 1,000 it overshoots by a quarter, and even in long ranges the found-to-estimate ratio wobbles by a few percent in both directions. A count that differs from the estimate is normal; a count that differs from the sieve is a mistake.
- Assuming a mid-range prime is a twin because its neighbour is odd. Most primes are isolated. In the first million only 16,337 of the 78,498 primes belong to a twin pair, and every odd neighbour that is not prime is a composite number with an odd factor you can find in a second.
The sieve method, the (6n − 1, 6n + 1) form and the Hardy–Littlewood formula are laid out on Wolfram MathWorld’s twin primes reference page; Brun’s constant and its partial sums are documented under Brun’s theorem; and the bounded-gap results are Zhang’s Bounded gaps between primes (Annals of Mathematics, 2014) and the Polymath paper Variants of the Selberg sieve that reached 246. Every count in this article was produced by the calculator above and cross-checked against independent primality tests.



