Solve problems 129 and 130.
This commit is contained in:
58
python/e129.py
Normal file
58
python/e129.py
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
from math import gcd
|
||||||
|
from functools import lru_cache
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache
|
||||||
|
def r(n):
|
||||||
|
assert n > 0
|
||||||
|
if n == 1:
|
||||||
|
return 1
|
||||||
|
else:
|
||||||
|
return 10**(n - 1) + r(n - 1)
|
||||||
|
|
||||||
|
|
||||||
|
def r_closed_form(n):
|
||||||
|
assert n > 0
|
||||||
|
return (10**n - 1) // 9
|
||||||
|
|
||||||
|
|
||||||
|
def r_modulo_closed_form(n, m):
|
||||||
|
assert n > 0 and m > 0
|
||||||
|
return ((pow(10, n, 9 * m) - 1) // 9) % m
|
||||||
|
|
||||||
|
|
||||||
|
def a(n):
|
||||||
|
assert gcd(n, 10) == 1
|
||||||
|
k = 1
|
||||||
|
while True:
|
||||||
|
if r_modulo_closed_form(k, n) == 0:
|
||||||
|
return k
|
||||||
|
k += 1
|
||||||
|
|
||||||
|
|
||||||
|
def euler_129():
|
||||||
|
|
||||||
|
# Comparing naiv to efficient implementations to find potential bugs.
|
||||||
|
for n in range(5, 1000):
|
||||||
|
assert r(n) == r_closed_form(n)
|
||||||
|
for m in range(2, 20):
|
||||||
|
assert (r_closed_form(n) % m) == r_modulo_closed_form(n, m)
|
||||||
|
|
||||||
|
assert a(7) == 6
|
||||||
|
assert a(41) == 5
|
||||||
|
assert a(17) == 16
|
||||||
|
|
||||||
|
target = 10**6
|
||||||
|
delta = 200
|
||||||
|
for n in range(target, target + delta):
|
||||||
|
if gcd(n, 10) == 1:
|
||||||
|
if (av := a(n)):
|
||||||
|
if av > target:
|
||||||
|
return n
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
solution = euler_129()
|
||||||
|
print("e129.py: " + str(solution))
|
||||||
|
assert(solution == 1000023)
|
||||||
32
python/e130.py
Normal file
32
python/e130.py
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
from math import gcd
|
||||||
|
from lib_prime import is_prime_rabin_miller
|
||||||
|
|
||||||
|
|
||||||
|
def r_modulo_closed_form(n, m):
|
||||||
|
assert n > 0 and m > 0
|
||||||
|
return ((pow(10, n, 9 * m) - 1) // 9) % m
|
||||||
|
|
||||||
|
|
||||||
|
def a_special(n):
|
||||||
|
k = n - 1
|
||||||
|
if r_modulo_closed_form(k, n) == 0:
|
||||||
|
return k
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def euler_130():
|
||||||
|
c, r = 0, 0
|
||||||
|
for n in range(2, 10**9):
|
||||||
|
if gcd(n, 10) == 1 and not is_prime_rabin_miller(n):
|
||||||
|
if a_special(n) is not None:
|
||||||
|
r += n
|
||||||
|
c += 1
|
||||||
|
if c == 25:
|
||||||
|
break
|
||||||
|
return r
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
solution = euler_130()
|
||||||
|
print("e130.py: " + str(solution))
|
||||||
|
assert(solution == 149253)
|
||||||
Reference in New Issue
Block a user