-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModular.java
More file actions
61 lines (61 loc) · 1.87 KB
/
Modular.java
File metadata and controls
61 lines (61 loc) · 1.87 KB
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
package FirstPkg;
import java.util.ArrayList;
import java.util.Scanner;
public class ModularSolver {
public static int gcd(int a, int b) {
int gcd;
if (a == 0)
gcd = b;
else if (b == 0)
gcd = a;
else
while (a != b) {
if (a > b)
a -= b;
else
b -= a;
}
gcd = a;
return gcd;
}
public static void solver() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a value: ");
int a_value = scanner.nextInt();
System.out.print("Enter b value: ");
int b_value = scanner.nextInt();
System.out.print("Enter mod value: ");
int mod_value = scanner.nextInt();
ArrayList <Integer> answer = new ArrayList<Integer> ();
int sol_count = 0, initial_sol = 0;
int d = gcd(a_value, mod_value);
if (d == 1)
sol_count = 1;
else if (d != 1 && b_value % d == 0)
sol_count = d;
else {
System.out.println("Solution is not possible");
sol_count = 0;
}
if (sol_count >= 1) {
int temp = 1;
int i = 1;
while (temp != 0) {
temp = ((a_value * i) - b_value) % mod_value;
i++;
}
i--;
initial_sol = i;
System.out.println("Initial solution: " + initial_sol);
for (int t = 0; answer.size()<d && temp < mod_value; t++) {
temp = initial_sol + ((mod_value / d) * t);
answer.add(temp);
}
System.out.println("Solutions: " + answer);
}
scanner.close();
}
public static void main(String[] args) {
solver();
}
}