What's a semiprime sandwich
Years ago, after a casual conversation with my friend Chai about aging and powers of 2 (specifically ), I stumbled on a neat observation:
The numbers were all consecutive, distinct semiprimes. And it begged the question: what is the longest possible chain of consecutive semiprimes? (Spoiler alert: it’s 3).
Ok before we continue, some vocab to job our memories.
A semiprime is a product of exactly two primes. The two primes can be equal () or different ().
A distinct semiprime is the second case: two different primes. So is 15 a distinct semiprime? Yes! Is 25 a distinct semiprime? No. For the purposes of this section, I say “semiprime” without qualifying it I mean the distinct kind. The non-distinct kind (squares of primes) breaks half the patterns we’re about to consider, so I’ll mostly ignore it.
A semiprime sandwich 🥪 is what I call sequences such as . Consecutive, distinct semiprimes, of length 3 (which, as we will show soon enough, is the longest possible chain of consecutive semiprimes. And the first occurance of such a sequence happens to be: .
why three is the ceiling
Time to actually show why 3 is the max. Note: I’m not labeling this a proof. The reasoning below stays pretty informal, but it’s tight enough to convince.
The claim: there’s no four consecutive integers where every one of them is a distinct semiprime.
Two cases to consider, depending on whether we anchor on an odd or even distinct semiprime.
Case 1: is an odd distinct semiprime. Both neighbors and are even.
Now suppose is also a distinct semiprime. The only way an even number is a distinct semiprime is for its factorization to look like for some odd prime . So , which means .
Look at . It’s . But is an odd prime, so is even, which means , and that’s divisible by 4. A distinct semiprime can’t be divisible by 4: that puts in the factorization (the prime 2 appearing twice), which breaks “distinct.”
So is not a distinct semiprime. The takeaway: given an odd distinct semiprime, either or is a distinct semiprime, but not both.
Case 2: is an even distinct semiprime. Same logic as above: the only way an even number is a distinct semiprime is where is an odd prime.
Look at . Since is an odd prime, is even, so is divisible by 4. Same kill, can’t be a distinct semiprime.
By the same logic, is divisible by 4 too. So neither nor is a distinct semiprime.
Putting it together. Among any four consecutive integers, exactly one of them is divisible by 4 (residues mod 4 cycle through , hitting each one once). That divisible-by-4 slot can’t be a distinct semiprime. Case 1 catches it if we anchored on an odd, Case 2 catches it if we anchored on an even. Either way the chain breaks at that slot.
So no four-in-a-row, and the max chain length is 3. ∎
By the way: a useful side effect of the proof is that the middle slice of any sandwich is always the even one. The two outside slices are odd (each a product of two different odd primes), and the middle is the unique even distinct semiprime in the triple.
That smallest sandwich also happens to land in a real chunk of human ages. So if you’re 33, 34, or 35 right now: you’re literally inside a sandwich. (Next sandwich birthday after that? See Puzzle 2 below. Heads up: it’s a long wait.)
Five puzzles. They get harder.
The middle of a sandwich is even. Sharpen that: show the middle is actually for some odd prime . Then show by walking through small primes.
~ show solution ~ ~ hide solution ~
Middle is . The middle is a distinct semiprime and even, so 2 is one of its two primes. The other prime has to be different from 2, so it’s odd. Call it . Middle is with an odd prime.
. Walk through small odd primes:
| triple | what kills it | ||
|---|---|---|---|
| 3 | 6 | 5 and 7 are primes | |
| 5 | 10 | , 11 prime | |
| 7 | 14 | 13 prime | |
| 11 | 22 | 23 prime | |
| 13 | 26 | , | |
| 17 | 34 | nothing kills it ✓ |
First that survives is 17. Smallest sandwich is . Anything smaller has at least one outer slice that’s a prime, a prime power, or otherwise not a distinct semiprime.
Find the next two sandwiches after by hand. No spreadsheets.
~ show solution ~ ~ hide solution ~
Middle is with odd prime (Puzzle 1). Walk candidate middles above 34 and check whether the two odd neighbors are both distinct semiprimes:
| middle | triple | result |
|---|---|---|
| 37 prime ✗ | ||
| ✗ | ||
| 59 prime ✗ | ||
| 61 prime ✗ | ||
| 73 prime ✗ | ||
| ✗ | ||
| , ✓ |
Next sandwich: .
Keep going:
| | | , ✓ |
Sandwich after that: .
So if you’re stuck at 33–35 right now, your next sandwich birthdays are 85–87, then 93–95. About 50 years to the next one, then a quick 8 years to the one after. Sandwiches don’t space out evenly. They cluster in surprising places.
Show the middle of any sandwich is never divisible by 3. The factor of 3 always lives in an outer slice.
~ show solution ~ ~ hide solution ~
Suppose the middle is divisible by 3. It’s also even (Puzzle 1), so it’s divisible by both 2 and 3, hence by 6. Middle is for some .
But the middle is a distinct semiprime: exactly two different primes. We see 2 and 3 both dividing it, so those are its two primes. No room for anything else. Middle has to be .
If the middle is 6, the sandwich is . 5 and 7 are primes, not semiprimes. So no sandwich has middle 6, and no sandwich has middle divisible by 3.
Now use “exactly one of three consecutive integers is divisible by 3.” The middle isn’t, so the factor of 3 has to be in the bottom or the top.
This is the seed of the Class A / Class B classification (more in §2).
We argued the max chain length is 3 above. Make the argument maximally tight: for any , prove that at least one of fails to be a distinct semiprime, without splitting into “n is odd” / “n is even” cases. (Hint: just look at which integer is divisible by 4 directly.)
~ show solution ~ ~ hide solution ~
Among any four consecutive integers, exactly one is divisible by 4 (residues mod 4 cycle through once in any 4-window). Call it .
If is a distinct semiprime, then with both prime. But means 2 appears in ‘s factorization with multiplicity , forcing . That contradicts .
So is not a distinct semiprime. The chain of four breaks at . Max sandwich length is 3. ∎
Bonus. The “wall” (the four-window’s slot that always breaks) is whichever integer is divisible by 4. So in any sandwich , one of its immediate neighbors or is divisible by 4. That’s the wall blocking extension. Useful for Puzzle 5.
Twin sandwich: and both sandwiches, with just the single integer as a wall between them. Six distinct semiprimes packed into seven consecutive integers. Real or fake? Find one or argue they can’t exist.
~ show solution ~ ~ hide solution ~
They exist, but rare. Smallest is at and :
Wall: . That’s the wall from Puzzle 4 (the one in the 4-window divisible by 4).
Neat fact: , so . Not a coincidence: every twin-sandwich wall is divisible by 36. Quick sketch: both middles are for primes , so the wall inherits a factor of 4 (since is odd, is even). A class-structure argument from §2 forces another factor of 9. Multiply: 36.
The twin sandwich question is also way harder than it looks. In §2 I’ll show that proving infinitely many twin sandwiches exist would imply the twin prime conjecture, open since 1849. So while twin sandwiches do exist, proving they keep happening is at least as hard as one of the famous open problems in number theory.
Class A and Class B
Where does the factor of 3 live in a sandwich? Let me check the first three:
In it’s the bottom. In it’s the top. In back in the bottom. The factor of 3 jumps around between the two outer slices.
Why never the middle? We showed in Puzzle 3 of §1: the middle of any sandwich is never divisible by 3. So the factor of 3 has exactly two places it can live: the bottom or the top. That gives us the binary classification this section is about.
A semiprime sandwich is Class A if 3 divides the bottom slice, and Class B if 3 divides the top slice. Every sandwich is exactly one of these. Going by the table above: is Class A, is Class B, is Class A.
the linear couplings
Each class has a clean algebraic shape. The middle is always for an odd prime (Puzzle 1 of §1). The factor-of-3 slice is for an odd prime . Lining the two facts up:
| Class | bottom | middle | top |
|---|---|---|---|
| A | |||
| B |
Class A: the middle is exactly one more than the bottom, giving . Class B: the middle is exactly one less than the top, giving .
These are the linear couplings. They tie the two auxiliary primes ( in the middle, in the factor-of-3 slice) together with one easy equation each. Pretty much every modular constraint we’ll see falls out by reducing one of these mod something small.
Identify and for each of the first three sandwiches, name the class, and verify the linear coupling for that class.
~ show solution ~ ~ hide solution ~
| sandwich | class | check | ||
|---|---|---|---|---|
| (33, 34, 35) | 17 | 11 | A | , ✓ |
| (85, 86, 87) | 43 | 29 | B | , ✓ |
| (93, 94, 95) | 47 | 31 | A | , ✓ |
detect the class from p alone
Small but useful observation. If you know just the middle prime , you can decide the class by looking at .
Show that a sandwich is Class A iff , and Class B iff .
~ show solution ~ ~ hide solution ~
Reduce each linear coupling mod 3.
Class A. . The right side is , so . The inverse of 2 mod 3 is itself (), so .
Class B. . The left side is , so , i.e. , giving .
Spot check. : , so Class A ✓. : , so Class B ✓.
square sandwiches are always Class B
A square sandwich is one whose bottom slice is the square of a prime: for prime. The motivating example is one. Most of the rest of this pamphlet is about square sandwiches specifically.
Quick fact about them: every square sandwich is Class B.
If it were Class A, then 3 would divide the bottom , which means 3 divides (since 3 is prime). The only prime divisible by 3 is 3 itself, giving the triple . But 11 is prime, not a semiprime. So no Class A square sandwich exists.
Square sandwiches are always Team Class B.
you can’t pack two sandwiches back-to-back
How tightly can two sandwiches sit? Could you have and ? Six distinct semiprimes in a row?
No. The middles would be and , both even (Puzzle 1 of §1). But they differ by 3, which is odd. Two evens can’t differ by an odd number. Contradiction.
So sandwiches always have at least one non-semiprime integer between them. The tightest possible configuration is the twin sandwich: and , separated by the lone integer . Six distinct semiprimes among seven consecutive integers.
We met the smallest twin sandwich in Puzzle 5 of §1: and , with separator .
Show that if and are both sandwiches, then the separator is divisible by 4.
~ show solution ~ ~ hide solution ~
The two middles are and for primes (both odd, since both ).
The separator is . Since is odd, is even, so is divisible by . ✓
A more careful argument using the class structure of both sandwiches actually shows . Schoenfield observed this on OEIS sequence A202319. We’ll skip the mod-9 piece. The takeaway: twin sandwiches are very rare and pinned to a sparse residue class.
twin sandwiches are at least as hard as twin primes
Punchline of this section. Proving twin sandwich infinitude is at least as hard as the famous twin prime conjecture (open since 1849).
Suppose and are both sandwiches. Each carries a factor of 3 in one of its outer slices (Class A or Class B). The two factor-of-3 slices land at one of these distances apart on the integer line:
| sandwich 1 | sandwich 2 | factor-of-3 distance |
|---|---|---|
| Class A (3 in bottom ) | Class A (3 in bottom ) | 4 |
| Class A (3 in bottom ) | Class B (3 in top ) | 6 |
| Class B (3 in top ) | Class A (3 in bottom ) | 2 |
| Class B (3 in top ) | Class B (3 in top ) | 4 |
The two factor-of-3 slices look like and with primes. So has to be one of . Only has an integer solution, giving .
So the two -primes form a twin prime pair. An infinite family of twin sandwiches gives you an infinite family of twin primes for free.
The twin prime conjecture is open. So twin sandwich infinitude is open in a strictly harder way.
Above I claimed only the (Class A, Class B) pairing at distance 6 works (with ). Argue this directly: show that if both sandwiches are the same class (both A or both B), the resulting equation has no integer solution.
~ show solution ~ ~ hide solution ~
Two Class A: factors of 3 sit at the bottoms and , distance 4. So , i.e. . No integer makes . ✗
Two Class B: factors of 3 sit at the tops and , distance 4. Same equation . No integer solution. ✗
So twin sandwiches must mix classes. From the table, both mixed pairings give , i.e. the two -primes are twin primes.
but square sandwiches don’t twin
Surprise: square sandwiches never appear in twin configuration. Empirically, zero clusters across all 4,668 square sandwiches with .
Show that if is a square sandwich with prime, then is not a sandwich.
~ show solution ~ ~ hide solution ~
The starting square sandwich is Class B (we proved this above), so , which gives .
Look at the candidate next triple mod 3:
The middle of this candidate is divisible by 3. But the middle of any sandwich is never divisible by 3 (Puzzle 3 of §1). So the candidate isn’t a sandwich. ✓
Square sandwiches are isolated. They never appear in twin configuration.
The square-centered case
For the rest of this pamphlet I’ll narrow focus to square sandwiches: sandwiches whose bottom slice is the square of a prime. Like where .
Two facts about square sandwiches we already have:
- The bottom is , a non-distinct semiprime. (That’s OK. The original “distinct” condition is really about the middle and top, and we’ll keep enforcing it there.)
- The sandwich is Class B (we proved this in §2). The factor of 3 lives in the top.
So for any prime , the candidate sandwich looks like
with ( prime, since the middle is from Puzzle 1 of §1) and ( prime, Class B). Solving:
Subtract the equations: , giving . That’s the Class B linear coupling we already met in §2.
The whole sandwich is determined by . The sandwich exists iff , , and are all prime.
the smallest examples by hand
Let me walk through small primes and compute :
| sandwich? | ||||
|---|---|---|---|---|
| 3 | 9 | 5 | (not integer) | ✗ |
| 5 | 25 | 13 | 9 (not prime) | ✗ |
| 7 | 49 | 25 = 5² (not prime) | 17 | ✗ |
| 11 | 121 | 61 | 41 | ✓ |
| 13 | 169 | 85 = 5·17 (not prime) | 57 = 3·19 (not prime) | ✗ |
| 17 | 289 | 145 = 5·29 (not prime) | 97 | ✗ |
| 19 | 361 | 181 | 121 = 11² (not prime) | ✗ |
| 23 | 529 | 265 = 5·53 (not prime) | 177 = 3·59 (not prime) | ✗ |
| 29 | 841 | 421 | 281 | ✓ |
Sparse. Among up to 29, only 11 and 29 produce sandwiches. So 121 and 841 are the first two prime squares that center a sandwich.
modular constraints, slowly
Some patterns are visible in the table. Several values fail in similar ways. Which can possibly produce a sandwich at all?
The trick is to take each defining equation and reduce it modulo a small prime. Three reductions are enough to pin down the picture: mod 3, mod 4, mod 5.
mod 4
is an odd prime, so or . In either case (just check : their squares are , all ).
So , which means .
The middle prime is always one more than a multiple of 4.
mod 3
is prime and , so or . Squaring kills the sign: .
So , and . To divide by 2 mod 3, multiply by 2’s inverse, which is 2 (since ). So .
This also matches what §2 said: square sandwiches are Class B, and Class B forces .
mod 5
is prime and , so or . Squaring: .
If , then , which means , so . But is prime , contradiction. So .
Then . Inverse of 2 mod 5 is 3 (since ). So .
gluing them together
We’ve shown , , . The Chinese Remainder Theorem (CRT) says: residue conditions modulo coprime numbers can be glued. If are pairwise coprime, knowing mod each pins down mod .
Here 3, 4, 5 are pairwise coprime, with product 60. Each condition is "", which glues to:
Every middle prime in a square sandwich is one more than a multiple of 60. Spot check: . All . ✓
Verify for .
~ show solution ~ ~ hide solution ~
: . ✓
: . ✓
: . ✓
the four lanes mod 30
The mod-5 step pinned , which means or . Combine with the obvious ” is prime ” (so coprime to 2, 3, 5), and we can read off all surviving residues modulo .
Primes live in residue classes coprime to 30, which are . Eight classes. Demanding or kills four of them, leaving:
Half of the eight prime-compatible residue classes mod 30 are eliminated by this one observation, before any computer search starts.
Verify the lane restriction by checking, for each of , that the middle slice ends up divisible by 5 (which kills from being prime once ).
~ show solution ~ ~ hide solution ~
Reduce each residue mod 5:
- : .
- : .
- : .
- : .
All four have , so , so . That means , so . But has to be prime. Contradiction. ✓
the constraint on b
A similar analysis on (using , which rearranges to ) gives:
The key ingredient is that has to be a quadratic residue modulo : there has to be some integer with . By the supplementary laws of quadratic reciprocity (don’t worry, you don’t need to know the proof to use the result), this happens iff or . Combined with the mod-3 piece (Class B forces ), the answer mod 24 is exactly .
We’ll come back to this in §6 with a much cleaner explanation in the language of the ring .
the brute force says
Searching primes in the four valid lanes turns up 35 sandwiches. The split between and runs 18 to 17. Nearly even. About 5.7% of primes in valid lanes actually produce a sandwich. Sparse but not vanishingly so.
The two clean small examples again:
- : , . Triple .
- : , . Triple .
The next valid after 29 is 79. Compute , , and check that all three are prime.
~ show solution ~ ~ hide solution ~
.
. To check prime, divide by primes up to : . None divide 3121. So is prime. ✓
. Divide by primes up to : same list up through 43. None divide 2081. So is prime. ✓
Lane check on : , so . One of the four valid lanes. ✓
Mod-60 check on : , so . ✓
Mod-24 check on : , so . ✓
The triple is .
Verify for . Which lane does each fall into?
~ show solution ~ ~ hide solution ~
: . (Lane 17.)
: . (Lane 17.)
: . (Lane 17.)
All three of the smallest valid values land in Lane 17. The first with is , where . So Lane 1 takes a while to show up.
Try . From the table above, , not prime. Show that is special: it solves the negative Pell equation for some integer . Find , and explain why any solution to the negative Pell equation forces to be a perfect square (so not prime, except trivially).
~ show solution ~ ~ hide solution ~
Try : . ✓
So , which means . The middle “prime” is a perfect square. The only prime perfect square would be… well, there are none. (A prime has no nontrivial divisors, but has as a divisor for .)
So every that solves automatically fails the sandwich condition at the middle. The negative Pell equation has infinitely many solutions: . Free list of values that can never produce a sandwich. More on this in §6.
The Gaussian reframe
This is the part that surprised me. To say what’s going on we need a quick detour through the Gaussian integers.
a number system one step beyond ℤ
The Gaussian integers, written , are the complex numbers of the form
where is the imaginary unit (). You can add and multiply Gaussian integers using the usual rules, treating as a formal symbol that squares to . Examples:
So is a number system you can add, multiply, and (with care) factor in. Just like the ordinary integers, but in two dimensions instead of one. Geometrically: a lattice of points in the complex plane.
The role that “absolute value” plays for is played here by the norm:
The norm is always a non-negative integer, and crucially it’s multiplicative:
So if factors as , then . The norm factors too. That’s the lever for everything below: knowing how integer primes factor in tells you about norms, and knowing about norms tells you about sums of squares.
Gaussian primes and Fermat’s two-squares theorem
A Gaussian prime is the analogue of an ordinary prime: a Gaussian integer that can’t be written as for non-trivial (where “non-trivial” means not multiples of or , the four units in ).
The structure of Gaussian primes is governed by Fermat’s old theorem on sums of two squares.
An odd prime can be written as for some integers if and only if .
In the language of :
- Primes split as for a Gaussian prime of norm .
- Primes stay prime in (their norm is ).
- The prime is a special “ramified” case.
So whenever you spot a prime , you also spot a Gaussian prime of norm sitting in the lattice. Now back to sandwiches.
the centered-square identity
Set (every odd has this form, with ), and look at the middle prime of the sandwich:
Now watch this:
So the middle prime is always a sum of two consecutive squares.
These primes (sums of two consecutive squares) are the centered square primes, OEIS A027862.
Spot check. , so , so . ✓ , so , so . ✓
the Gaussian integer π_k
Whenever is a sum of two squares, shows up as a norm in . In our case:
So we have a Gaussian integer
whose norm is the middle prime. And whenever is actually prime in , the element is a Gaussian prime. (Why , which Fermat’s theorem requires: exactly one of is even, so is even-square + odd-square .)
Geometrically, sits one step above the diagonal in the lattice. On the line .
Show that is a Gaussian prime if and only if is a rational prime.
~ show solution ~ ~ hide solution ~
(⇐) If is a rational prime, then is a Gaussian prime. We have (computed above). By Fermat’s two-squares theorem, splits in as where is a Gaussian prime of norm . Since and the Gaussian primes of norm are exactly and up to units, is one of these. Gaussian prime ✓.
(⇒) If is a Gaussian prime, then is a rational prime. The norm of any Gaussian prime is either a rational prime or the square of a rational prime . We computed , ruling out the latter. So is a rational prime.
the (1+i) reformulation
Here’s a small but beautiful identity. In :
But . So:
The sandwich-generating prime shows up as the imaginary part of , with real part identically . Multiplying by in is a 45° rotation followed by a scaling by , so this identity says: rotate by 45° and scale up, and you land on the vertical line at height .
A one-parameter Gaussian-arithmetic encoding of the entire square-sandwich structure. Every valid corresponds to a Gaussian prime such that , with .
the geometric picture
The argument (angle) of is
So sandwich-generating Gaussian primes don’t appear randomly across the lattice. They spiral toward the diagonal as grows, hugging the line from above and landing closer and closer to a 45° angle.
| 11 | 5 | ||
| 29 | 14 | ||
| 79 | 39 | ||
| 271 | 135 |
Picture: an infinite sequence of dots on the line , starting visibly above the diagonal and slowly settling onto it, picking out the centered-square primes as you go. The places where the dot isn’t a Gaussian prime (like ) are exactly where the would-be middle prime turns out to be composite.
why r = 7 fails, Gaussian-style
At , we have , so . The norm is . By Puzzle 1 above, since isn’t a rational prime, isn’t a Gaussian prime either.
Concretely, factors as
(Quick check: . ✓) The factor is itself a Gaussian prime (norm , prime), so is a Gaussian prime squared, not a Gaussian prime itself.
The would-be middle prime at is , which is composite. The side and the side fail in lockstep: doesn’t stay irreducible in , doesn’t stay prime in . There’s a deeper reason this happens at specifically (it’s a solution to the negative Pell equation with ). More on that in §6.
the small-k failures
The construction starts working at (giving ). For , at least one of the three sandwich conditions fails.
For each of , identify which condition fails:
- prime,
- prime,
- prime.
~ show solution ~ ~ hide solution ~
k = 1, r = 3. Triple is . We have ✓. But is prime, not a semiprime. The factor of 3 isn’t in any slice (the bottom has 3 but isn’t a distinct semiprime). Fails.
k = 2, r = 5. Triple is . Middle ✓. Top is not a distinct semiprime ( is not prime). Fails.
k = 3, r = 7. Triple is . Top ✓. Middle , not a distinct semiprime ( is not prime). This is the Pell failure: isn’t a Gaussian prime.
k = 4, r = 9. is not prime. Fails at step 1.
Smallest where all three conditions hold: , giving and the motivating triple .
The first ten valid values:
Corresponding middle primes: . All centered square primes (forced by the construction).
Verify for (so , ).
~ show solution ~ ~ hide solution ~
Compute :
And , so . ✓
The points all lie on the line in the Gaussian lattice. Show that the next parallel line contains no Gaussian primes at all.
~ show solution ~ ~ hide solution ~
For , the norm is
Always even, and , with equality only at (giving , a unit times ).
For , is even and , so for some . By multiplicativity of the norm, has to factor non-trivially. So isn’t a Gaussian prime.
The line is the unique slope-1 line in the lattice along which Gaussian primes appear infinitely often. The line has the parity baked in, killing every candidate.
Above I claimed . Verify by direct computation. Then check that is a Gaussian prime, and explain why being a square of a Gaussian prime (rather than a Gaussian prime itself) is exactly what makes composite.
~ show solution ~ ~ hide solution ~
Direct computation. . ✓
is a Gaussian prime. Its norm is , which is a rational prime. The norm of a Gaussian prime is either a rational prime or the square of a rational prime . The norm 5 is a rational prime, so is a Gaussian prime.
Why is composite. . So is the square of a rational prime, hence composite.
The structural pattern: being a Gaussian prime ⟺ being a rational prime. When factors in , factors in . The two factorizations are linked.