Part 1
Description
Solution
The codes are written into a huge grid along diagonals, and each code comes from the one before it: multiply by 252533, then keep the remainder after dividing by 33554393. The first code is 20151125. I need the code at one row and column.
Checking the order on a small grid
First I recreated the first seven diagonals in Sheet1 and compared them with the puzzle’s example table. All 29 numbers matched.
Why I didn’t fill the grid
My cell is on a diagonal numbered above 6,000, which makes it roughly the 18-millionth code. No sheet can fill 18 million cells by hand, so I computed the position and then the code directly.
Finding the position
The code in row r and column c is on diagonal d = r + c − 1. All the earlier diagonals hold 1 + 2 + … + (d − 1) codes, and within the diagonal the code is number c. So the position is n = (d − 1)·d/2 + c, and I need to apply the rule n − 1 times from the first code, which means:
code = 20151125 × 252533^(n−1) mod 33554393
In Sheet2:
B1: my row B2: my column
B3: =B1+B2-1 ← diagonal
B4: =(B3-1)*B3/2+B2 ← position n
B5: =B4-1 ← exponent
B6: 33554393 B7: 252533 B8: 20151125
Raising to a big power
252533 to a power of millions is far too large for Excel, but I can use square-and-multiply. Take the exponent in binary. Row k of a table holds 252533^(2^k) mod 33554393, found by squaring the row above. When bit k of the exponent is 1, that row’s value is multiplied into a running result:
A11: 0 A12: =A11+1 (fill down to A40)
B11: =$B$7 B12: =MOD(B11*B11,$B$6)
C11: =MOD(INT($B$5/2^A11),2)
D11: =IF(C11=1,MOD(B11,$B$6),1)
D12: =IF(C12=1,MOD(D11*B12,$B$6),D11)
B9: =MOD($B$8*D40,$B$6) ← the code
I filled the formulas down to row 40, which covers exponents up to about a billion. B9 is the answer.
This stays exact because every product is below 2^53 (the biggest here is about 9.7×10^14, and Excel is exact up to 9×10^15). I think older Excel versions also need the quotient inside MOD to stay under 2^27 (about 134 million), and here it never goes past about 29 million.
To test it, row 1, column 1 gives 20151125, row 2, column 1 gives 31916031, and row 6, column 6 gives 27995004, as in the puzzle’s example.
A second check with VBA
To be sure, I also ran the sequence the slow way. It’s only about 18 million steps, a couple of seconds in VBA. The values stay below 2^53, so Double arithmetic is exact:
Public Sub Day25Check() Dim code As Double, i As Long, n As Long n = Range("B4").Value ' the position computed above code = 20151125# For i = 2 To n code = code * 252533# - Int(code * 252533# / 33554393#) * 33554393# Next i Debug.Print code End Sub
Both ways gave the same code, and that’s the answer I submitted.
0 komentar:
Post a Comment