-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdivisors.java
77 lines (72 loc) · 1.83 KB
/
divisors.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
73
74
75
76
77
class Divisors {
long[] findDivisors(long a, long b) {
long[] ans = { 0, 0 };
long i, j;
int d;
for (i = a; i <= b; i++) {
d = 1;
for (j = 1; j <= (i/2); j++) {
if (i % j == 0) {
d++;
}
}
if (d > ans[1]) {
ans[0] = i;
ans[1] = d;
}
}
return (ans);
}
}
class NewThread extends Thread {
Divisors ob;
long num, div, a, b;
NewThread(long a, long b, Divisors obj) {
super();
this.ob = obj;
num = 0;
div = 0;
this.a = a;
this.b = b;
start();
}
public void run() {
long[] ans = new long[2];
ans = ob.findDivisors(a, b);
this.num = ans[0];
this.div = ans[1];
}
long[] getAnswer() {
long[] ans = new long[2];
ans[0] = num;
ans[1] = div;
return (ans);
}
}
class q1 {
public static void main(String[] args) {
Divisors ob = new Divisors();
long num = 0, div = 0, p = 1, q = 10000;
long[] ans = new long[2];
NewThread nobj;
while (q <= 100000) {
nobj = new NewThread(p, q, ob);
try {
//nobj.join();
if (Thread.currentThread().getName().equals("main")) {
Thread.sleep(6000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
ans = nobj.getAnswer();
if (ans[1] > div) {
num = ans[0];
div = ans[1];
}
p += 10000;
q += 10000;
}
System.out.println("The number with maximum divisors : " + num + " and it has " + div + " divisors.");
}
}