Solve problem 346 in Python

main
Felix Martin 2021-07-05 15:09:24 -04:00
parent 470ecb3dc3
commit bff7ae0371
1 changed files with 23 additions and 0 deletions

23
python/e346.py Normal file
View File

@ -0,0 +1,23 @@
def euler_346():
n_max = 10**12
repunits = set()
result = 1 # Our algorithm doesn't catch 1 so we add it here
for base in range(2, n_max):
n = base ** 0 + base ** 1
for exp in range(2, n_max):
n += base**exp
if exp == 2 and n > n_max:
# There are no more repunits below n_max.
return result
if n >= n_max:
break
if not n in repunits:
repunits.add(n)
result += n
if __name__ == "__main__":
solution = euler_346()
print("e346.py: " + str(solution))
assert(solution == 336108797689259276)