Compare commits
2 Commits
1ba50f4c3a
...
0d650182d7
| Author | SHA1 | Date | |
|---|---|---|---|
| 0d650182d7 | |||
| de07bfb630 |
36
python/e206.py
Normal file
36
python/e206.py
Normal file
@@ -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)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
FROM = 88
|
||||
TILL = 90
|
||||
FROM = 206
|
||||
TILL = 206
|
||||
|
||||
template = """
|
||||
def euler_XXX():
|
||||
@@ -14,8 +14,12 @@ if __name__ == "__main__":
|
||||
|
||||
|
||||
for i in range(FROM, TILL + 1):
|
||||
e = "0" + str(i)
|
||||
if i < 100:
|
||||
e = "0" + str(i)
|
||||
else:
|
||||
e = str(i)
|
||||
filename = "e" + e + ".py"
|
||||
with open(filename, "w") as f:
|
||||
s = template.replace("XXX", e)
|
||||
f.write(s)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user