File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ def gcd (a , b ):
2
+ if b == 0 :
3
+ return a
4
+ else :
5
+ return gcd (b , a % b )
6
+
7
+ def modular_multiplicative_inverse (a , m ):
8
+ for x in range (1 , m ):
9
+ if (a * x ) % m == 1 :
10
+ return x
11
+ return None
12
+
13
+ def CRT (congruences ):
14
+ N = 1
15
+ for _ , ni in congruences :
16
+ N *= ni
17
+
18
+ x = 0
19
+ for ri , ni in congruences :
20
+ Ni = N // ni
21
+ Mi = modular_multiplicative_inverse (Ni , ni )
22
+ x += ri * Ni * Mi
23
+
24
+ return x % N
25
+
26
+ def get_congruencies ():
27
+ congruences = []
28
+ num_congruences = int (input ("Enter the number of congruencies" ))
29
+
30
+ for i in range (num_congruences ):
31
+ ri = int (input (f"Enter the Remainder r{ i + 1 } " ))
32
+ mi = int (input (f"Enter the modulus m{ i + 1 } " ))
33
+ congruences .append ((ri ,mi ))
34
+
35
+ return congruences
36
+
37
+ congruences = get_congruencies ()
38
+ solution = CRT (congruences )
39
+ print ("The solution to the system of congruences is:" , solution )
You can’t perform that action at this time.
0 commit comments