Calcudoku

Fill the grid with 1 to n so every row and every column holds each number once. Heavy outlines mark cages: the numbers in a cage combine to the target with the cage's operation. Minus and divide only ever label two-cell cages, and a number may repeat inside a cage as long as the repeats share no row or column. Click a cell, then type or use the keypad.

the board

where you are

your numbers last hint repeat in a line candidates left

The obvious way to read a square off the Jacobson–Matthews chain is biased

Every board starts as a Latin square, and the square should be uniform over all of them — otherwise the bank quietly favours some structures over others. The standard tool is the Jacobson–Matthews chain (1996): treat the square as an n × n × n cube of 0s and 1s with exactly one 1 on every line, and let each move add and subtract 1 on the eight corners of a sub-box. Some moves leave a single -1 behind — an improper cube, not a square — and the next move starts from that cell. The chain is uniform over proper cubes at stationarity.

The obvious way to read a square off it is: run a while, and if the cube happens to be improper, keep stepping until it is proper. That is biased, and the bias is not a mixing problem — running ten times longer does not remove it. On 7 × 7, waiting for the first proper cube after 343 moves gives a mean of 10.110 intercalates against an exact 10.529; after 3,430 moves, 10.107. Total variation from the exact distribution is 0.0473 and 0.0472, against a noise floor of 0.0101 for 20,000 perfect draws.

The reason is where the square is sampled. Waiting for the chain to become proper samples it at the moment it re-enters the proper squares, and a square is re-entered from an improper cube more often when it has fewer proper-to-proper moves. A proper-to-proper move is exactly an intercalate swap — a 2 × 2 subsquare a b / b a flipped to b a / a b: the move lands on a proper cube only when the eighth corner of its box was already a 1, and those eight corners are the four cells of an intercalate. A square with k intercalates has 4k such moves out of n³ − n², so squares with few intercalates are left by the improper route, and re-entered by it, more often — and they are over-drawn. The fix is to condition at a fixed time instead: run exactly 343 moves, keep the square only if the cube is proper at that moment, otherwise throw the chain away. That lands at 10.570 (total variation 0.0105), and it costs 6.83 chains per square: the stationary chain is proper only about 15% of the time.

The naive generator — fill cells in reading order with shuffled values, backtrack on a dead end — is biased too, in the same direction: 9.909 intercalates on 7 × 7 (total variation 0.0659). Why it leans that way is a separate question; the table only shows that it does.

At every order measured, the fixed-time reader sits inside the 95th percentile of the noise floor and the first-proper reader sits outside it. The fixed-time reader is what the generator uses; the other two are kept only as foils.

gridsamplermean intercalatesexact meanshare with noneexact sharetotal variationnoise floor (mean / 95th pct)chains per square
5 × 5naive backtracking3.3943.57115.16%10.71%0.04450.0015 / 0.0035—
5 × 5J–M, 125 moves, then wait for a proper cube3.4753.57113.13%10.71%0.02420.0015 / 0.0035—
5 × 5J–M, 1,250 moves, then wait for a proper cube3.4543.57113.64%10.71%0.02930.0015 / 0.0035—
5 × 5J–M, exactly 5 moves, keep only if proper3.2063.57119.86%10.71%0.09140.0015 / 0.00355.87
5 × 5J–M, exactly 25 moves, keep only if proper3.5703.57110.76%10.71%0.00040.0015 / 0.00355.26
5 × 5J–M, exactly 125 moves, keep only if proper3.5803.57110.51%10.71%0.00200.0015 / 0.00355.31
6 × 6naive backtracking7.3488.2650.73%0.43%0.09420.0065 / 0.0095—
6 × 6J–M, 216 moves, then wait for a proper cube7.5138.2650.44%0.43%0.07690.0065 / 0.0095—
6 × 6J–M, 2,160 moves, then wait for a proper cube7.4438.2650.47%0.43%0.08270.0065 / 0.0095—
6 × 6J–M, exactly 6 moves, keep only if proper7.3458.2651.38%0.43%0.48520.0065 / 0.00954.73
6 × 6J–M, exactly 36 moves, keep only if proper8.1658.2650.49%0.43%0.00990.0065 / 0.00955.67
6 × 6J–M, exactly 216 moves, keep only if proper8.2638.2650.43%0.43%0.00860.0065 / 0.00955.66
7 × 7naive backtracking9.90910.5290.15%0.10%0.06590.0101 / 0.0145—
7 × 7J–M, 343 moves, then wait for a proper cube10.11010.5290.12%0.10%0.04730.0101 / 0.0145—
7 × 7J–M, 3,430 moves, then wait for a proper cube10.10710.5290.14%0.10%0.04720.0101 / 0.0145—
7 × 7J–M, exactly 7 moves, keep only if proper7.68510.52914.83%0.10%0.46260.0101 / 0.014510.77
7 × 7J–M, exactly 49 moves, keep only if proper10.57010.5290.14%0.10%0.01140.0101 / 0.01456.80
7 × 7J–M, exactly 343 moves, keep only if proper10.57010.5290.09%0.10%0.01050.0101 / 0.01456.83

The census every sampler is held to

"Exact" above means exact: every reduced Latin square of order 1 to 7 is enumerated — first row and first column in order — and its intercalates counted. Permuting rows and columns never changes the intercalate count, and every square is a row-and-column permutation of exactly one reduced square in exactly n!(n-1)! ways, so the reduced histogram is also the distribution over all squares. Order 7 is 16,942,080 reduced squares (61,479,419,904,000 in all) and takes 41 s.

The counts match OEIS A000315 and A002860 at every order, and the stats run recomputes orders 1 to 6 from scratch each time and stops if the stored histogram disagrees. An intercalate count of 2 × 2 subsquares costs n per row pair: send each column to the column where the second row holds the same symbol, and an intercalate is exactly a 2-cycle of that map.

orderreduced squaresall squaresOEIS agreesmean intercalatesshare with nonemost intercalates
212yes1.0000.00%1 (100.0000%)
3112yes0.000100.00%0 (100.0000%)
44576yes6.0000.00%12 (25.0000%)
556161,280yes3.57110.71%4 (89.2857%)
69,408812,851,200yes8.2650.43%27 (0.2126%)
716,942,08061,479,419,904,000yes10.5290.10%42 (0.0012%)

An intercalate is a second answer waiting for a cage to miss it

Flip an intercalate and the square is still Latin. So if no cage notices the flip — every cage touched by the four cells still reaches its target — the board has a second answer, whatever else is true of it. A cage misses the flip when it holds one cell from each diagonal of the 2 × 2 (the multiset of its numbers does not change), or when it is a two-cell minus or divide cage whose other number happens to sit at the same distance, or the same ratio, from both symbols.

That makes a check that costs nothing next to a search. Across 16,000 boards cut at random, 8,704 came out with more than one answer, and on 6,658 of them (76.5%) a flipped intercalate was already one of the extra answers. The claim in the other direction is a theorem, so it is tested rather than sampled: 0 boards with a surviving flip ever came out unique.

It also predicts which squares make good boards. On 16 of 16 rows below, the squares whose raw cut came out unique had fewer intercalates on average than the squares whose cut did not. A generator that draws a uniform square, cuts it, and keeps only the cuts that come out unique would therefore ship a bank that leans away from intercalates — a biased sample of Latin squares, even with a perfect sampler underneath.

Counts are capped at 50 answers and 100,000 search nodes; a capped search counts as not unique, and the medians are over searches that finished.

gridlargest cageboardscages (mean)uniquemore than one answerof those, a flip explains itintercalates when uniqueintercalates when notexact mean
4 × 421,0009.6515 (51.5%)485404 (83.3%)5.15 ± 0.127.12 ± 0.186.00
4 × 431,0008.0437 (43.7%)563509 (90.4%)4.59 ± 0.107.23 ± 0.176.00
4 × 441,0007.4375 (37.5%)625544 (87.0%)4.55 ± 0.117.07 ± 0.166.00
4 × 451,0007.0349 (34.9%)651559 (85.9%)4.55 ± 0.116.64 ± 0.156.00
5 × 521,00014.9647 (64.7%)353239 (67.7%)3.41 ± 0.063.86 ± 0.043.57
5 × 531,00012.4574 (57.4%)426288 (67.6%)3.23 ± 0.073.91 ± 0.033.57
5 × 541,00011.4471 (47.1%)529322 (60.9%)3.29 ± 0.073.77 ± 0.043.57
5 × 551,00010.7409 (40.9%)591356 (60.2%)3.41 ± 0.073.78 ± 0.043.57
6 × 621,00021.4580 (58.0%)420335 (79.8%)7.31 ± 0.169.51 ± 0.258.27
6 × 631,00017.9478 (47.8%)522382 (73.2%)7.13 ± 0.189.57 ± 0.238.27
6 × 641,00016.3397 (39.7%)603465 (77.1%)6.93 ± 0.189.36 ± 0.208.27
6 × 651,00015.5315 (31.5%)685505 (73.7%)6.84 ± 0.219.05 ± 0.188.27
7 × 721,00029.2622 (62.2%)378297 (78.6%)9.83 ± 0.1411.17 ± 0.1910.53
7 × 731,00024.3433 (43.3%)567444 (78.3%)9.63 ± 0.1611.28 ± 0.1610.53
7 × 741,00022.1377 (37.7%)623488 (78.3%)9.40 ± 0.1711.34 ± 0.1510.53
7 × 751,00020.8317 (31.7%)683521 (76.3%)9.92 ± 0.1911.08 ± 0.1410.53

Repair, don’t reject — and the bank keeps the sampler’s uniformity

So this generator never throws a square away for being hard to pin. It cuts random cages (sizes weighted towards two and three cells), labels them, and then repairs: while a second answer exists, it splits a cage that holds a cell where the two answers differ. Once the board is unique it tightens: it tries merging neighbouring cages and keeps a merge only if the answer stays unique, until a whole pass of merges is refused. Every search is node-capped, and a capped search refuses the candidate rather than guessing.

Repair always terminates — splitting enough cages turns every cell into a one-cell cage, which pins the answer outright — so every seed yields a board: 40/40, 40/40, 40/40, 40/40, 40/40, 40/40 across the runs below. Because nothing is rejected, the squares in the bank are exactly the squares the sampler drew. The mean intercalate count of the generated boards is within two standard errors of the exact mean on 3 of 3 sizes.

The intercalate check runs before every search. Over the runs with it switched on, it found 93 of 144 repairs (64.6%) without a search. It did not make the generator faster, and the table says so: on 7 × 7 the median board took 206 ms with the check and 224 ms without, and the worst board 26,448 ms against 12,514 ms. Search nodes are close either way (20,566 against 18,622 on 7 × 7), and the wall clock is set by a few slow boards that the node count does not explain. The check stays because it is a proof, not a speed-up: it names the second answer, and the four cells it names are where the split goes.

gridintercalate checkboards / seedsrepairs found by the checkrepairs found by searchsearch nodesms per board (median / worst)merges keptcells per cageintercalates (bank)exact mean
5 × 5on40 / 402452633 / 153.582.873.70 ± 0.173.57
5 × 5off40 / 400303973 / 93.672.893.70 ± 0.173.57
6 × 6on40 / 4030173,19138 / 1,3256.833.548.18 ± 0.708.27
6 × 6off40 / 400493,74452 / 1,5006.903.558.18 ± 0.708.27
7 × 7on40 / 40392920,566206 / 26,4489.553.6011.03 ± 0.8110.53
7 × 7off40 / 4007018,622224 / 12,5149.723.6511.03 ± 0.8110.53

What an operation sign is worth

Some puzzle books sell Calcudoku with the signs left off as the harder version. Hiding a sign is a clean ablation: the same cages, the same targets, and a cage now holds if any operation its size allows reaches the target. Hiding can only add answers, never remove one, and the test suite checks both that and that every rung stays sound under both readings.

Cage by cage, the sign is worth little. Of 4,041 multi-cell cages cut at random, 2,043 (50.6%) admit exactly the same fillings with the sign hidden. The most informative sign is the quotient on a 2-cell cage: hiding it multiplies the fillings by 2.74 on average (1.46 bits). No 2-cell quotient cage survived hiding untouched: a quotient target is always reachable some other way too. On cages of four or more cells the sum and the product rarely collide, and the sign carries at most 0.11 bits.

Board by board the loss is small too. Over the same 16,000 random cuts as above, 7,296 were unique with the signs shown and 6,711 with them hidden: hiding every sign costs 8.0% of the unique boards. On the shipped bank, which was tightened with the signs shown, 19 of 20 boards still have exactly one answer with every sign hidden — the exception is 7x7-20, which opens up to 2 answers. Tick hide the signs on the page and try it.

cagecagesfillings with the signfillings withoutbits the sign carriesno change at all
2 cells, -1,0597.6310.090.468472 (44.6%)
2 cells, +3003.866.270.68597 (32.3%)
2 cells, ÷3503.519.231.4550 (0.0%)
2 cells, ×3002.325.300.931131 (43.7%)
3 cells, +58715.6319.130.287234 (39.9%)
3 cells, ×6247.2010.610.367458 (73.4%)
4 cells, +28467.8070.390.071172 (60.6%)
4 cells, ×29425.9327.390.106277 (94.2%)
5 cells, +125334.05336.220.01785 (68.0%)
5 cells, ×11887.5489.970.044117 (99.2%)
gridlargest cageboardsunique, signs shownunique, signs hiddenmore answers once hiddenmedian answers shown / hidden
4 × 421,000515438156 of 1,0001 / 2
4 × 431,000437377146 of 1,0002 / 2
4 × 441,000375332134 of 1,0002 / 2
4 × 451,000349312115 of 1,0002 / 2
5 × 521,00064758697 of 1,0001 / 1
5 × 531,00057453776 of 1,0001 / 1
5 × 541,00047144388 of 1,0002 / 2
5 × 551,00040938386 of 1,0002 / 2
6 × 621,000580524122 of 1,0001 / 1
6 × 631,00047844886 of 1,0002 / 2
6 × 641,00039736495 of 1,0002 / 2
6 × 651,00031529487 of 1,0002 / 2
7 × 721,00062258262 of 1,0001 / 1
7 × 731,00043341553 of 1,0002 / 2
7 × 741,00037737132 of 1,0002 / 2
7 × 751,00031730548 of 1,0002 / 2

The ladder, rung by rung

Each rung is priced twice on the 20 shipped boards (630 cells): climbing — how much the ladder settles with everything up to that rung — and leave-one-out — what the full ladder loses without it, and how much harder the search works without it.

Two things stand out. single is a special case of must: a settled cell sits in some cage, every filling of that cage puts the cell's number on the cell's row and column, so must already takes it off the rest of both lines outside the cage, and cage takes it off the cells inside. Leaving single out of the search leaves the node count exactly where it was, at 149. It stays on the ladder because it is the step a person takes first. Second, leave any one rung out and the rest still finish every board with no search, except probe: drop probe and 14 of 20 finish. The search itself leans on must and hidden: without them it needs 459 and 229 nodes.

rungcells settled, climbingboards finished, climbingboards finished without itsearch nodes without it
cage43 (6.8%)0 / 2020 / 20—
single122 (19.4%)4 / 2020 / 20149 (with it: 149)
hidden257 (40.8%)9 / 2020 / 20229 (with it: 149)
must411 (65.2%)13 / 2020 / 20459 (with it: 149)
subset453 (71.9%)14 / 2020 / 20—
probe630 (100.0%)20 / 2014 / 20—