Add more solutions

This commit is contained in:
Kevin Keogh
2019-02-03 14:21:43 -05:00
parent 4ef32a7e04
commit fbebd6c05f
7 changed files with 147 additions and 0 deletions

60
problem-69.c Normal file
View File

@@ -0,0 +1,60 @@
#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;
}