-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChineseRemainder.java
58 lines (49 loc) · 1.3 KB
/
ChineseRemainder.java
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
import java.util.Scanner;
public class ChineseRemainder
{
int n;
int rem[];
int div[];
public static void main(String[] args)
{
ChineseRemainder remain = new ChineseRemainder();
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of remainder-divisor pairs: ");
remain.n = sc.nextInt();
remain.rem = new int[remain.n];
remain.div = new int[remain.n];
System.out.println("\nEnter Remainders: ");
for(int i = 0; i<remain.n; i++)
{
remain.rem[i] = sc.nextInt();
}
System.out.println("Enter Divisors: ");
for(int i = 0; i<remain.n; i++)
{
remain.div[i] = sc.nextInt();
}
int solution = remain.remainder();
if (solution != -1)
System.out.println("The number is: " + solution);
else
System.out.println("No solution exists.");
sc.close();
}
public int remainder()
{
int x = 1,i = 0;
while(true)
{
for(i = 0; i<n;i++)
{
if(x % div[i] != rem[i])
break;
}
if(i == n)
{
return x;
}
x++;
}
}
}