-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExample12.java
72 lines (52 loc) · 2.05 KB
/
Example12.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package applications.algorithms;
import java.util.ArrayList;
/** Category: Algorithms
* ID: Example12
* Description: Compute the equivalence classes of a list of numbers
* Taken From: Matters Computational: https://www.jjj.de/fxt/fxtbook.pdf by Jorg Arndt
* Details:
* TODO
*/
public class Example12 {
/**
* Choose an integer m ≥ 1
* Two integers a and b will be equivalent if a − b is an integer multiple of m (with m = 1 all integers are in the same class).
* We can choose the numbers 0, 1 . . . , m − 1 as representatives of the m classes obtained. See the book mentioned above
*/
public static boolean modulo_equivalence(int a, int b, int modulo){
if((a-b)%modulo == 0){
return true;
}
return false;
}
public static ArrayList<Integer> compute_equivalnce(ArrayList<Integer> list, int modulo){
ArrayList<Integer> equivalence = new ArrayList<>(list.size());
// each integer initially goes in its own class
for (int i = 0; i < list.size(); i++) {
equivalence.add(i);
}
for (int i = 1; i < list.size(); i++) {
int j = 0;
while(! Example12.modulo_equivalence(list.get(i), list.get(j), modulo)){
++j;
}
equivalence.set(i, equivalence.get(j));
}
return equivalence;
}
public static void main(String[] args){
ArrayList<Integer> list = new ArrayList<>(20);
for (int i = 0; i < 20; i++) {
list.add(i);
}
ArrayList<Integer> even = Example12.compute_equivalnce(list, 2);
for (int i = 0; i < even.size(); i++) {
System.out.println("For integer "+list.get(i)+" equivalence class is: "+even.get(i));
}
System.out.println(" ");
ArrayList<Integer> multipleOfThree = Example12.compute_equivalnce(list, 3);
for (int i = 0; i < even.size(); i++) {
System.out.println("For integer "+list.get(i)+" equivalence class is: "+multipleOfThree.get(i));
}
}
}