forked from iRupam/NewtonSchoolInfinityJune21
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumOfGCD.java
More file actions
50 lines (39 loc) · 1.13 KB
/
SumOfGCD.java
File metadata and controls
50 lines (39 loc) · 1.13 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
package InfinityJune21.DoubtSessionJuly24;
public class SumOfGCD {
static int getCount(int d, int n) {
int no = n / d;
int result = no;
for(int p = 2; p * p <= no; p++) {
if(no % p == 0) {
while(no % p == 0) {
no = no / p;
}
result = result - (result / p);
}
}
if(no > 1) {
result = result - (result / no);
}
return result;
}
static int getSumOfGCDOfPairs(int n) {
int result = 0;
for(int i = 1; i * i <= n; i++) {
if(n % i == 0) {
int d1 = i;
int d2 = n / i;
result = result + (d1 * getCount(d1, n));
if(d1 != d2) {
result = result + (d2 * getCount(d2, n));
}
}
}
return result;
}
public static void main(String[] args) {
int n = 6546;
int sum = 0;
sum = getSumOfGCDOfPairs(n);
System.out.println("Sum is: " + sum);
}
}