Use sieve method instead of algebraic method

This commit is contained in:
Peter J. Holzer 2020-12-14 22:02:32 +01:00 committed by Peter J. Holzer
parent 272523b8b2
commit 259f974f32
1 changed files with 26 additions and 44 deletions

View File

@ -1,50 +1,32 @@
#!/usr/bin/python3 #!/usr/bin/python3
import time
def ext_euclid(a, b): def shuttle_generator():
""" with open("input") as fh:
returns solutions x and y for equation ax + by = gcd(a, b) now = int(fh.readline())
""" for i, x in enumerate(fh.readline().split(",")):
if x != "x":
x = int(x)
yield i, x
(old_r, r) = (a, b) shuttle = shuttle_generator()
(old_s, s) = (1, 0)
(old_t, t) = (0, 1)
while r: def check(t, tests):
quotient = old_r // r for i, n in tests:
(old_r, r) = (r, old_r - quotient * r) if (t + i) % n != 0:
(old_s, s) = (s, old_s - quotient * s) return False
(old_t, t) = (t, old_t - quotient * t) return True
return old_s, old_t, old_r
n = [] t = 0
a = [] step = 1
with open("input") as fh: n = 1
now = int(fh.readline()) tests = []
for i, x in enumerate(fh.readline().split(",")): while True:
if x != "x": if check(t, tests):
x = int(x) print(t, "matches", tests)
n.append(x) time.sleep(2)
a.append((x - i) % x) step *= n
i, n = next(shuttle)
tests.append((i, n))
t += step
print(n)
print(a)
n1 = n.pop()
n2 = n.pop()
a1 = a.pop()
a2 = a.pop()
m1, m2, _ = ext_euclid(n1, n2)
assert _ == 1
x = a1 * m2 * n2 + a2 * m1 * n1
print(x)
while n:
n1 = n1 * n2
a1 = x
n2 = n.pop()
a2 = a.pop()
m1, m2, _ = ext_euclid(n1, n2)
assert _ == 1
x = a1 * m2 * n2 + a2 * m1 * n1
print(x)