-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathChinese_remainder.py
53 lines (42 loc) · 1.61 KB
/
Chinese_remainder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""-----------------------------------------------------Introduction------------------------------------------------------------------------*/
/*The Chinese Remainder Theorem is used to solve modular arithmetic problems efficiently.Modular arithmetic involves performing
arithmetic operations on remainders. For example, if we divide 13 by 5, the remainder is 3. In modular arithmetic notation, we
would write this as 13 ≡ 3 (mod 5), which means that 13 is congruent to 3 modulo 5."""
"""-----------------------------------------------------Algorithm----------------------------------------------------------------------------"""
"""start with 1 and one by one increment it and check if dividing it with given elements in
num[] produces corresponding remainders in rem[]. Once we find such an x, we return it. """
# A Python3 program to demonstrate
# working of Chinise remainder Theorem
#Function to find the smallest no that will produce respective remainder.
def findMinX(num, rem, k):
x = 1 # Initialize result
while(True):
# Check if remainder of
# x % num[j] is rem[j]
# or not (for all j from
# 0 to k-1)
j = 0
while(j < k):
if (x % num[j] != rem[j]):
break
j += 1
# If all remainders
# matched, we found x
if (j == k):
return x
# Else try next number
x += 1
# Driver Code
num=[]
n=int(input("Number of elements in num array:"))
for i in range(0,n):
g=int(input())
num.append(g)
#rem = [2, 3, 1]
rem=[]
m=int(input("Number of elements in rem array:"))
for i in range(0,m):
t=int(input())
rem.append(t)
k = len(num)
print("x is", findMinX(num, rem, k))