-
Notifications
You must be signed in to change notification settings - Fork 3
/
CommonPrimeDivisors.java
45 lines (40 loc) · 1.14 KB
/
CommonPrimeDivisors.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
/*
Check whether two numbers have the same prime divisors.
*/
class Solution {
public int solution(int[] A, int[] B) {
int count = 0;
for (int i = 0; i < A.length; i++) {
int num1 = A[i];
int num2 = B[i];
int gcd = gcd_func(num1, num2);
int max = Math.max(num1, num2);
int min = Math.min(num1, num2);
int gcd1 = gcd;
while (max != 1) {
gcd1 = gcd_func(max, gcd1);
if (gcd1 == 1) break;
max = max/gcd1;
}
if (max != 1) continue;
int gcd2 = gcd;
while (min != 1) {
gcd2 = gcd_func(min, gcd2);
if (gcd2 == 1) break;
min = min/gcd2;
}
if (min != 1) continue;
count++;
}
return count;
}
int gcd_func(int a, int b) {
if (a > b) {
if (a % b == 0) return b;
return gcd_func(b, a % b);
} else {
if (b % a == 0) return a;
return gcd_func(a, b % a);
}
}
}