Solve 2020 day 13 CRT jojo
This commit is contained in:
15
lib.py
15
lib.py
@@ -280,3 +280,18 @@ def get_data(filename):
|
||||
assert os.path.isfile(txt_file), "Could not download AoC file"
|
||||
with open(txt_file) as f:
|
||||
return f.read()
|
||||
|
||||
|
||||
def mod_inverse(a, m):
|
||||
def egcd(a, b):
|
||||
if a == 0:
|
||||
return b, 0, 1
|
||||
else:
|
||||
g, y, x = egcd(b % a, a)
|
||||
return g, x - (b // a) * y, y
|
||||
|
||||
g, x, _ = egcd(a, m)
|
||||
if g != 1:
|
||||
raise Exception('Modular inverse does not exist')
|
||||
else:
|
||||
return x % m
|
||||
|
||||
Reference in New Issue
Block a user