Python – How to solve the congruence system in python?

How to solve the congruence system in python?… here is a solution to the problem.

How to solve the congruence system in python?

For the issue with Ax ≡ B (MOD C), I DID THAT, NO PROBLEM :

 def congru(a,b,c):
    for i in range(0,c):
       if ((a*i - b)%c)== 0 :
          print(i)

Now I have to solve a system of equations where A = ( 5x + 7y) and A = (6x + 2y),
and B= 4 and B = 12, respectively, where C is 26.

In other words:
( 5x + 7y)≡ 4 (mod 26)
(6x + 2y)≡ 12 (mod 26)

What should I do?

Thank you.

Solution

For aX ≡ b (mod m) linear congruence, here’s a more powerful Python solution based on Euler’s theorem that works well even for very large numbers:

def linear_congruence(a, b, m):
    if b == 0:
        return 0

if a < 0:
        a = -a
        b = -b

b %= m
    while a > m:
        a -= m

return (m * linear_congruence(m, -b, a) + b) // a

>>> linear_congruence(80484954784936, 69992716484293, 119315717514047)
>>> 45347150615590

For X,

Y systems: multiply the first equation by 2 and the second equation by -7, add them together and solve for X. Then replace X and solve Y.

Related Problems and Solutions