61 lines
940 B
C
61 lines
940 B
C
#include <stdio.h>
|
|
|
|
unsigned int gcd(unsigned int u, unsigned int v)
|
|
{
|
|
int shift;
|
|
if (u == 0) return v;
|
|
if (v == 0) return u;
|
|
shift = __builtin_ctz(u | v);
|
|
u >>= __builtin_ctz(u);
|
|
do {
|
|
v >>= __builtin_ctz(v);
|
|
if (u > v) {
|
|
unsigned int t = v;
|
|
v = u;
|
|
u = t;
|
|
}
|
|
v = v - u;
|
|
} while (v != 0);
|
|
return u << shift;
|
|
}
|
|
|
|
|
|
unsigned int is_coprime(unsigned int u, unsigned int v)
|
|
{
|
|
if (gcd(u, v) == 1)
|
|
return 1;
|
|
return 0;
|
|
}
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
unsigned int i, j;
|
|
unsigned int coprimes;
|
|
unsigned int MAX_NUM = 1000000;
|
|
int greatest = 0;
|
|
double ratio = 0;
|
|
|
|
for (i=1; i<MAX_NUM; i++) {
|
|
if (i % 10000 == 0)
|
|
printf("%i\n", i);
|
|
|
|
coprimes = 1;
|
|
for (j = 2; j<i; j++) {
|
|
if (is_coprime(i, j))
|
|
coprimes += 1;
|
|
}
|
|
|
|
if (((double)i / coprimes) > greatest) {
|
|
greatest = i;
|
|
ratio = (double)i / coprimes;
|
|
}
|
|
}
|
|
|
|
printf("number: %i\n", greatest);
|
|
printf("ratio : %f\n", ratio);
|
|
return 1;
|
|
}
|
|
|
|
|
|
|