Project Euler Problem 206

Find the unique positive integer whose square has the form 1234567890, where each “” is a single digit.

Project Euler Problem 206

Solution

Answer: 1389019170

We seek the unique positive integer $n$ such that

$$n^2 = 1_2_3_4_5_6_7_8_9_0$$

where each underscore is an arbitrary digit.

That means the square must have the decimal pattern

$$1x2x3x4x5x6x7x8x9x0$$

for digits $x\in{0,\dots,9}$.


Step 1 — Understanding the structure

The number ends in $0$, so:

$$n^2 \equiv 0 \pmod{10}$$

Hence $n$ must end in $0$.

Write

$$n = 10m$$

Then

$$n^2 = 100m^2$$

Removing the final two zeros from the square pattern gives:

$$m^2 = 1_2_3_4_5_6_7_8_9$$

More importantly, since the original square ends with $900$,

$$n^2 \equiv 900 \pmod{1000}$$

Thus

$$n^2 \equiv 9 \pmod{10}$$

so $n/10$ must end in $3$ or $7$, because:

$$3^2 \equiv 9 \pmod{10},\qquad 7^2 \equiv 9 \pmod{10}$$

Therefore:

$$n \text{ ends in } 30 \text{ or } 70$$

This already reduces the search space enormously.


Step 2 — Determine the numerical range

The smallest possible square is

$$1020304050607080900$$

and the largest is

$$1929394959697989990$$

So:

$$\sqrt{1020304050607080900} \le n \le \sqrt{1929394959697989990}$$

Computing approximately:

$$1010101010 \lesssim n \lesssim 1389026623$$

Since $n$ must end in $30$ or $70$, we only test those candidates.


Step 3 — Efficient pattern checking

Instead of converting to strings repeatedly, we can inspect digits directly.

Suppose

$$s = n^2$$

We need:

  • units digit = 0
  • hundreds digit = 9
  • ten-thousands digit = 8
  • etc.

Equivalently, reading from right to left:

$$0,9,8,7,6,5,4,3,2,1$$

with one arbitrary digit between each required digit.

A convenient method is string matching:

str(s)[::2] == "1234567890"

because the required digits occupy every other position.


Step 4 — Python implementation

import math

# Lower and upper bounds for the square root
low = int(math.isqrt(1020304050607080900))
high = int(math.isqrt(1929394959697989990)) + 1

# Force the search to numbers ending in 30 or 70
for n in range(low, high, 10):

    # Only numbers ending in 30 or 70 are possible
    if n % 100 not in (30, 70):
        continue

    sq = n * n

    # Check the required pattern
    if str(sq)[::2] == "1234567890":
        print(n)
        break

Step 5 — Code walkthrough

Bounds

low = int(math.isqrt(...))
high = int(math.isqrt(...)) + 1

These compute the smallest and largest possible square roots.


Stepping by 10

for n in range(low, high, 10):

Since the square ends in 0, $n$ must end in 0.


Final two-digit restriction

if n % 100 not in (30, 70):
    continue

Only candidates ending in 30 or 70 can work.

This cuts the search space by another factor of 5.


Squaring

sq = n * n

Compute the square.


Pattern test

if str(sq)[::2] == "1234567890":

Example:

If

sq = 1929374254627488900

then

str(sq)[::2]

extracts digits:

$$1,2,3,4,5,6,7,8,9,0$$

exactly as required.


Step 6 — Result

Running the program produces:

$$n = 1389019170$$

Verification:

$$1389019170^2 = 1929374254627488900$$

Pattern:

$$1,9,2,9,3,7,4,2,5,4,6,2,7,4,8,8,9,0,0$$

Taking every other digit gives:

$$1,2,3,4,5,6,7,8,9,0$$

which matches perfectly.

The number also ends in $70$, consistent with the modular analysis.


Answer: 1389019170