-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
/
Copy pathsol1.py
53 lines (40 loc) · 1.46 KB
/
sol1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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() = }")