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.
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
import sys, os; sys.path.append(os.path.abspath('..'))
from timer import timethis
from math import sqrt
def get_digits(n):
digits = []
while n:
digits.append(n % 10)
n //= 100
return digits
get_digits(12345678901)
@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
solve()