Skip to content

Hacktoberfest: add project euler for problem 206 #3684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Empty file.
53 changes: 53 additions & 0 deletions project_euler/problem_206/sol1.py
Original file line number Diff line number Diff line change
@@ -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() = }")