All Euler problems
Project Euler

Concealed Square

Find the unique positive integer whose square has the form 1_2_3_4_5_6_7_8_9_0, where each underscore represents a single digit.

Source sync May 21, 2026
Problem #0206
Level Level 03
Solved By 26,712
Languages C++, Python
Answer 1389019170
Length 284 words
modular_arithmeticsearchnumber_theory

Problem Statement

This archive keeps the full statement, math, and original media on the page.

Find the unique positive integer whose square has the form \(1\_2\_3\_4\_5\_6\_7\_8\_9\_0\), where each “\(\_\)†is a single digit.

Problem 206: Concealed Square

Mathematical Development

Theorem (Divisibility by 10). If n2n^2 has the form 1_2_3_4_5_6_7_8_9_01\_2\_3\_4\_5\_6\_7\_8\_9\_0, then 10∣n10 \mid n.

Proof. The last digit of n2n^2 is 0, so nn must be divisible by both 2 and 5. Hence 10∣n10 \mid n. □\square

Theorem (Residue Constraint mod 100). Write n=10mn = 10m. Then n2n^2 has the prescribed form only if m≡3(mod10)m \equiv 3 \pmod{10} or m≡7(mod10)m \equiv 7 \pmod{10}, that is, n≡30(mod100)n \equiv 30 \pmod{100} or n≡70(mod100)n \equiv 70 \pmod{100}.

Proof. Since n2=100m2n^2 = 100m^2, the pattern forces the hundreds digit of n2n^2 to be 9, so the units digit of m2m^2 must be 9. A square ends in 9 exactly when its root ends in 3 or 7. □\square

Lemma (Search Bounds). The value of nn satisfies

1010101010≤n≤1389199190.1010101010 \leq n \leq 1389199190.

Proof. The 19-digit square n2n^2 lies in the interval [1020304050607080900,  1929394959697989990][1020304050607080900,\; 1929394959697989990]. Taking square roots gives

⌈1020304050607080900⌉=1010101010,⌊1929394959697989990⌋=1389199189.\left\lceil\sqrt{1020304050607080900}\right\rceil = 1010101010, \qquad \left\lfloor\sqrt{1929394959697989990}\right\rfloor = 1389199189.

Since nn must be divisible by 10, the upper endpoint rounds to 13891991901389199190. □\square

Theorem (Uniqueness). There exists exactly one nn in the search range satisfying the pattern constraint.

Proof. The search is exhaustive over the candidates with final digits 30 or 70 inside the square-root interval. Computational verification finds exactly one solution:

n=1389019170,n2=1929374254627488900,n = 1389019170, \qquad n^2 = 1929374254627488900,

which matches the pattern 1_2_3_4_5_6_7_8_9_01\_2\_3\_4\_5\_6\_7\_8\_9\_0. □\square

Editorial

The digit pattern forces strong modular restrictions before any search begins. Because the square ends in 0, the number itself must be divisible by 10. After dividing by 10, the next fixed digit forces the remaining factor to end in 3 or 7, so the full number must end in 30 or 70.

That leaves a narrow arithmetic progression inside the square-root bounds of the pattern interval. We scan those candidates and test the square digit by digit against 1_2_3_4_5_6_7_8_9_0. The search is still exhaustive, but the modular pruning makes it small enough to finish quickly.

Pseudocode

lower = ceil(sqrt(1020304050607080900))
upper = floor(sqrt(1929394959697989990))

Move lower upward to the first number in [lower, upper]
whose last two digits are 30 or 70.

candidate = lower
While candidate <= upper:
    square = candidate * candidate
    If the digits of square in positions
       1, 3, 5, ..., 19 are 1, 2, 3, ..., 9, 0 respectively:
        return candidate

    If candidate ends in 30:
        candidate += 40
    Otherwise:
        candidate += 60

Complexity Analysis

  • Time: O((nmax⁡−nmin⁡)/50)O((n_{\max} - n_{\min}) / 50) candidate checks, each with constant-time digit testing.
  • Space: O(1)O(1).

Answer

1389019170\boxed{1389019170}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_206/solution.cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    // We need n such that n^2 has the form 1_2_3_4_5_6_7_8_9_0
    // n must end in 30 or 70 (since n divisible by 10, and (n/10)^2 ends in 9)
    // n^2 has 19 digits, so n is roughly 1.01e9 to 1.39e9

    // Use __int128 or just unsigned long long (19 digits fits in ull)
    // max n^2 ~ 1.93e18 which fits in unsigned long long (max ~1.84e19)

    auto check = [](long long n) -> bool {
        long long sq = n * n;
        // Check pattern: digits at positions (from right):
        // pos 0 -> 0, pos 2 -> 9, pos 4 -> 8, ..., pos 18 -> 1
        // i.e., (sq / 10^(2k)) % 10 == (10 - k) for k=0..9, with digit 10-0=10->0 special
        // Actually: the pattern from left is 1_2_3_4_5_6_7_8_9_0
        // From right (position 0 = units): pos 0 = 0, pos 2 = 9, pos 4 = 8, ...
        // pos 2*i should be (10 - i) % 10 for i = 0..9

        for (int i = 0; i <= 9; i++) {
            int digit = sq % 10;
            int expected = (10 - i) % 10;
            if (digit != expected) return false;
            sq /= 100; // skip one digit (the underscore)
        }
        return true;
    };

    // Search range
    long long lo = (long long)ceil(sqrt(1020304050607080900.0));
    long long hi = (long long)floor(sqrt(1929394959697989990.0));

    // Adjust lo to end in 30 or 70
    for (long long n = lo; n <= hi; n++) {
        int r = n % 100;
        if (r == 30 || r == 70) {
            lo = n;
            break;
        }
    }

    for (long long n = lo; n <= hi; n += 10) {
        // n ends in X0, we want X=3 or X=7
        int tens = (n / 10) % 10;
        if (tens != 3 && tens != 7) continue;

        if (check(n)) {
            cout << n << endl;
            return 0;
        }
    }

    return 0;
}