forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chinese_Remainder_Theorem.cs
64 lines (56 loc) · 1.64 KB
/
Chinese_Remainder_Theorem.cs
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
53
54
55
56
57
58
59
60
61
62
63
64
// Calculating inverse using extended Euclidean algorithm iterative method
using System;
namespace Insertion_Sort
{
class Program
{
public static int inverse(int a, int m)
{
int m0 = m;
int x0 = 0;
int x1 = 1;
if (m == 1)
return 0;
int q, t;
while (a > 1)
{
q = a / m;
t = m;
m = a % m;
a = t;
t = x0;
x0 = x1 - q * x0;
x1 = t;
}
if (x1 < 0)
x1 += m0;
return x1;
}
public static int findMinimumDividend(int[] divisor, int[] remainder)
{
int product = 1;
int i, result, partialProduct;
int len = divisor.GetLength(0);
for (i = 0; i < len; i++)
product *= divisor[i];
result = 0;
for (i = 0; i < len; i++)
{
partialProduct = product / divisor[i];
result += remainder[i] * inverse(partialProduct, divisor[i]) * partialProduct;
}
return (result % product);
}
static void Main(String[] args)
{
int[] divisor = { 3, 4, 5, 7, 11 };
int[] remainder = { 2, 3, 1, 4, 5 };
int Result = findMinimumDividend(divisor, remainder);
Console.WriteLine("Minimum value of dividend is : " + Convert.ToString(Result));
Console.WriteLine();
Console.ReadLine();
}
}
}
// Output
// Minimum value of dividend is : 3371