Python Implementation of Problem 206

View source code here on GitHub!

Includes

Problem Solution

Project Euler Problem 206

This one ended up being a regex problem. I am sure there is a more clever way to go about it, but this way works fairly quickly.

Problem:

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.

python.src.p0206.main() int
 1"""
 2Project Euler Problem 206
 3
 4This one ended up being a regex problem. I am sure there is a more clever way
 5to go about it, but this way works fairly quickly.
 6
 7Problem:
 8
 9Find the unique positive integer whose square has the form 1_2_3_4_5_6_7_8_9_0,
10where each “_” is a single digit.
11"""
12from re import compile
13
14
15def main() -> int:
16    pattern = compile("1.2.3.4.5.6.7.8.9.0")
17    minimum = 1010101010  # sqrt of minimum number matching pattern
18    maximum = 1389026624  # sqrt of maximum number matching pattern
19    step = 10  # the square is a multiple of 10, which means it's a multiple of
20    # 100, since 2 and 5 are both prime. Therefore we can step by at least 10
21    for x in range(minimum, maximum, step):
22        if pattern.match(repr(x * x)):
23            return x
24    return -1  # pragma: no cover

Tags: square-number, square-root