-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11417_GCD.cpp
72 lines (66 loc) · 1.19 KB
/
11417_GCD.cpp
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
/*##### Md. Walid Bin khalid Aunkon #####*/
/*##### Sr. Software Engineer (Unity) #####*/
/*##### Email: [email protected] #####*/
/*##### Mobile No: +88-01916-492926 #####*/
#include <stdio.h>
const int maxRange = 501;
int GCDArray[maxRange][maxRange];
int Primes[maxRange];
int MemSet()
{
for (int i = 0; i < maxRange; i++)
{
Primes[i] = i;
for (int j = 0; j < maxRange; j++)
{
GCDArray[i][j] = 0;
}
}
return 0;
}
int PrimeSet()
{
MemSet();
for (int i = 2; i < maxRange; i++)
{
if (Primes[i] == i)
for (int j = i * 2; j < maxRange; j += i)
{
Primes[j] = 0;
}
}
return 0;
}
int GCD(int k, int l)
{
if (Primes[k] == k && Primes[l] == l)
return GCDArray[k][l] = 1;
if (GCDArray[k][l] != 0)
return GCDArray[k][l];
int gcd = 1;
for (int i = 1; i <= k && i <= l; i++)
{
if (k % i == 0 && l % i == 0)
gcd = gcd < i ? i : gcd;
}
GCDArray[k][l] = gcd;
return GCDArray[k][l];
}
int main()
{
PrimeSet();
int n;
while (scanf("%d", &n) && n != 0)
{
int g = 0;
for (int i = 1; i < n; i++)
{
for (int j = i + 1; j <= n; j++)
{
g += GCD(i, j);
}
}
printf("%d\n", g);
}
return 0;
}