From 0d650182d7452693b4f8e1725155aa079946723c Mon Sep 17 00:00:00 2001 From: Felix Martin Date: Mon, 19 Apr 2021 23:06:48 -0400 Subject: [PATCH] Solve problem 206 --- python/e206.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 python/e206.py diff --git a/python/e206.py b/python/e206.py new file mode 100644 index 0000000..91d59db --- /dev/null +++ b/python/e206.py @@ -0,0 +1,36 @@ +import math + + +def is_square(apositiveint): + """ From: https://stackoverflow.com/a/2489519 """ + x = apositiveint // 2 + seen = set([x]) + while x * x != apositiveint: + x = (x + (apositiveint // x)) // 2 + if x in seen: return False + seen.add(x) + return True + + +def canditates(current_value, depth): + if depth <= 0: + yield current_value + else: + d = 10 ** depth + for i in range(10): + for new_value in canditates(current_value - i * d, depth - 2): + yield new_value + + +def euler_206(): + c = 1929394959697989990 + for i, c in enumerate(canditates(c, 17)): + if is_square(c): + return int(math.sqrt(c)) + + +if __name__ == "__main__": + solution = euler_206() + print("e206.py: " + str(solution)) + assert(solution == 1389019170) +