MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / chinese_remainder_theorem2

Function chinese_remainder_theorem2

maths/chinese_remainder_theorem.py:75–87  ·  view source on GitHub ↗

>>> chinese_remainder_theorem2(5,1,7,3) 31 >>> chinese_remainder_theorem2(6,1,4,3) 14

(n1: int, r1: int, n2: int, r2: int)

Source from the content-addressed store, hash-verified

73
74# Same a above using InvertingModulo
75def chinese_remainder_theorem2(n1: int, r1: int, n2: int, r2: int) -> int:
76 """
77 >>> chinese_remainder_theorem2(5,1,7,3)
78 31
79
80 >>> chinese_remainder_theorem2(6,1,4,3)
81 14
82
83 """
84 x, y = invert_modulo(n1, n2), invert_modulo(n2, n1)
85 m = n1 * n2
86 n = r2 * x * n1 + r1 * y * n2
87 return (n % m + m) % m
88
89
90if __name__ == "__main__":

Callers

nothing calls this directly

Calls 1

invert_moduloFunction · 0.70

Tested by

no test coverage detected