# Find the unique passcode

"Is this the Coldport Lost Property Office? I think I left my puzzle-book on the train this morning, and I was hoping someone had handed it in." The speaker was a tall gentleman with a six-sided flat cap.

"It is," said Sally, who was (wo-)manning the desk that morning. She sighed slightly. "However, the door to the Lost Property is locked with a code, and I haven't figured out what the code is yet."

"Figured it out? You mean... it's a puzzle?" A gleam appeared in the gentleman's eye. "Tell me about it, please!"

"The code entry is seven digits," said Sally. "But the door is faulty, so the digit $$1$$ is always there first, and when you press a digit on the code pad, the pad automatically immediately enters another digit. It starts at $$1$$ and increments each time."

"So if you press $$6$$," said the gentleman, "the pad entry reads $$162$$?"

"And if you pressed six again, it would read $$16263$$?"

"Exactly. And all I've been told so far is that the code to be entered is the unique perfect square." She sighed again. "I don't see how that helps me."

The gentleman grinned. "Oh, but that should be sufficient," he said. "One moment...." He pulled out a pad of paper and started scribbling on it. It look slightly longer than a single moment, but ten minutes later he suggested a seven-digit code to Sally, and to her slight surprise, the door unlocked. From the smell that gusted out as she opened it, it had been locked for quite some time.

What is the seven digit code to unlock the door? I'm adding the no-computers tag to this as a brute-force search is quite easy, but using a calculator to find squares or square-roots is permitted. (A good solution will ideally describe how to go about solving it without a computer :) )

The number is

1721344.

First step:

We know that our number looks like 1x2y3z4. This has to be the square root of something between 1000 (whose square is 1000000) and approximately 1414 (whose square is about 2000000 because sqrt(2)~=1.414). In fact it can't begin 14 because then it would be at least 1960000, contradicting the 2 in its third digit. The last digit must be 2 or 8 for the square's last digit to be 4. At this point we have only about 5x10x2=100 possibilities, so brute force wouldn't be too bad even by hand, but let's try to do better.

Now a little bit of brute force, which takes a few minutes with a pocket calculator:

If our number begins 10 then its square is in the range [1000000,1210000); if it begins 11 then [1210000,1440000); if it begins 12 then [1440000,1690000); if it begins 13 then [1690000,1960000). So given that the third digit is 2, the options are 10->102, 10->112, 11->122, 11->132, 11->142, 12->152, 12->162, 13->172, 13->182, 13->192. Each of these implies a fairly narrow range of possibilities; e.g., if the square begins 112 then the number is between the square roots of 1120000 inclusive and 1130000 exclusive; that is, between 1058+ and 1063+, which with the last-digit constraint permits only 1062. (Whose square turns out not to have the right form. We can play the same game for the other possibilities. 102 -> 1009+..1014+ -> 1012 which doesn't work. 122 -> 1104+..1109+ -> 1108 which doesn't work. 132 -> 1148+..1153+ -> 1152 which doesn't work. 142 -> 1192+..1195+ -> no solutions at all. 152 -> 1232+..1236+ -> no solutions at all. 162 -> 1272+..1276+ -> no solutions at all. 172 -> 1311+..1315+ -> 1312 -> 1721344 which is our solution. We're told the answer is the unique square, so no need to check 182 and 192. Done.

• (I have the feeling it might be possible to reduce the amount of brute force a bit.) – Gareth McCaughan Jun 19 '20 at 14:00
• As fast as ever! Well done :) You can, by looking at the other end of the number, speed things up a bit more too: properties of squares quickly constrain the square-root to be of the form $1uv2$ or $1uv8$ and then considering the square of that you find that either $4v = z$ or $16v+6=z$. But this is an easy-to-follow way of doing it, and I like it! – user40528 Jun 19 '20 at 14:01