diff --git a/DIRECTORY.md b/DIRECTORY.md index f0f494e7a3b4..74af068fc268 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -712,6 +712,8 @@ * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_174/sol1.py) * Problem 191 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_191/sol1.py) + * Problem 206 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_206/sol1.py) * Problem 234 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_234/sol1.py) * Problem 551 diff --git a/project_euler/problem_206/__init__.py b/project_euler/problem_206/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_206/sol1.py b/project_euler/problem_206/sol1.py new file mode 100644 index 000000000000..d01e9e78d96a --- /dev/null +++ b/project_euler/problem_206/sol1.py @@ -0,0 +1,53 @@ +""" +Problem 206 : https://projecteuler.net/problem=206 + +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. + +Solution: Finding a square number can found by adding consecutive odd numbers +starting from 1. The minimum number possible for perfect square is 1020304050607080900 +in this situation, so started checking digits are in correct places after +total is above it. +""" + +import math + + +def solution() -> int: + """ + >>> solution() + 1389019170 + """ + + # adding odds + total = 1 + add = 1 + + while True: + + # adding odds + add += 2 + total += add + + # starts count here since the bottom limit + if total > 1020304050607080900: + + # nested if statements to check digits + num = str(total) + if int(num[-1]) == 0: + if int(num[-3]) == 9: + if int(num[-5]) == 8: + if int(num[-7]) == 7: + if int(num[-9]) == 6: + if int(num[-11]) == 5: + if int(num[-13]) == 4: + if int(num[-15]) == 3: + if int(num[-17]) == 2: + if int(num[-19]) == 1: + break + + return int(math.sqrt(total)) + + +if __name__ == "__main__": + print(f"{solution() = }")