Concealed Square

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.


Idea

First get the lower and upper bound of candiate integers, and can notice that result integer must end with '30' or '70' to get '9_0' pattern


In [1]:
import sys, os; sys.path.append(os.path.abspath('..'))
from timer import timethis
from math import sqrt
In [2]:
def get_digits(n):
    digits = []
    while n:
        digits.append(n % 10)
        n //= 100
    return digits
In [3]:
get_digits(12345678901)
Out[3]:
[1, 9, 7, 5, 3, 1]
In [4]:
@timethis
def solve():
    digit_number = 10 + 9
    rng = (int(sqrt(1020304050607080900)), int(sqrt(2 * pow(10, digit_number-1))))
    pattern = [0] + list(range(9, 0, -1))
    for i in range(rng[0] // 100, rng[1] // 100, 1):
        for j in [i*100+30, i*100+70]:
            n = pow(j, 2)
            if get_digits(n) == pattern:
                return j
In [5]:
solve()
Run for 24.609 seconds
Out[5]:
1389019170