-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFindJoinPoint.java
More file actions
78 lines (62 loc) · 1.62 KB
/
Copy pathFindJoinPoint.java
File metadata and controls
78 lines (62 loc) · 1.62 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
*
*/
package findJoinPoint;
import java.util.HashSet;
import java.util.Set;
/**
* @author TALBI
*
*/
public class FindJoinPoint {
final static long LIMIT = 20000000;
/**
* @param args
*/
public static void main(String[] args) {
int s1 = 11;
int s2 = 13;
long debut = System.currentTimeMillis();
System.out.println(findJointure(s1, s2));
long fin = System.currentTimeMillis();
System.out.println("executé en " + (fin - debut) + " milisecondes.");
System.out.println("opération");
System.out.println(01 | 11);
}
public static long findJointure(int s1, int s2) {
Set<Integer> suite1 = new HashSet<Integer>();
Set<Integer> suite2 = new HashSet<Integer>();
suite1.add(s1);
suite2.add(s2);
int tmp = 0;
if (s1 > s2) {
tmp = s1;
s1 = s2;
s2 = tmp;
}
Integer suivantSuite1 = new Integer(s1), suivantSuite2 = new Integer(s2);
for (; suite1.retainAll(suite2) && suivantSuite1 <= LIMIT && suivantSuite2 <= LIMIT;) {
suivantSuite1 = calculSuite(suivantSuite1);
suivantSuite2 = calculSuite(suivantSuite2);
suite1.add(suivantSuite1);
suite2.add(suivantSuite2);
}
for (Integer elt : suite1) {
return elt.longValue();
}
return -1;
}
private static Integer calculSuite(Integer suivantSuite) {
String text = suivantSuite.toString();
int somme = 0;
for (int index = 0; index < text.length(); index++) {
char aChar = text.charAt(index);
int digit = Character.getNumericValue(aChar);
somme += digit;
}
return suivantSuite + somme;
}
public static boolean intersection(Set<Integer> suite1, Set<Integer> suite2) {
return suite1.retainAll(suite2);
}
}