#include #include int gcd(int a,int b){ while( 1 ) { a = a % b; if( a == 0 ) return b; b = b % a; if( b == 0 ) return a; } } int main() { long long unsigned int i, j; long long unsigned int besti, bestj, bestg; long long unsigned int n = 0; double closeto = 3.0 / 7.0; double maxdist = 1; for (i = 4; i <= 1000000; i++) { j = i * 3.0 / 7.0 - 1; double maxj = (i * 3.5 / 7.0) + 1; if (j > 0) { for (j; j <= maxj; j++) { double frac = j / (i * 1.0); if (frac < closeto && (closeto - frac) < maxdist) { //reduce the fraction int g = gcd(i,j); if (g > 0) { maxdist = (closeto - frac); besti = i; bestj = j; bestg = g; } } } } if (i % 100000 == 0) { printf("%d / %d = %f (%f)\n",(int)(besti/bestg),(int)(bestj/bestg),bestj / (besti * 1.0),maxdist); printf ("%d\n",i); } } printf("done!\n"); printf("%d / %d = %f (%f)\n",(int)(bestj/bestg),(int)(besti/bestg),bestj / (besti * 1.0),maxdist); return 0; }