/* * 9a.c | Jim M | Jan 2013 * projecteuler.net #9, version a * * $ gcc 9a.c -o 9a * $ ./9a * ... * sum = 1000 : a=200, b=375, c=425 * * So far so good. * * Changing the "int sum = 1000;" line to make the sum bigger, * recompiling and rerunning gives these results: * * sum = 10000 : a=2000, b=3750, c=4250 * sum = 100000 : a=5408, b=69844, c=24748 b > c ??? WRONG!! * * The last result is clearly incorrect, since b is bigger than c. * But the code compiled and ran without warnings or errors. * * Your mission : explain what's going on - in detail. * */ #include int main(){ int a, b, c; int sum = 1000; printf("Looking for a,b,c such that a**2+b**2=c**2 and a+b+c=%i ...\n", sum); for (a = 1; a < sum - 2; a++){ for (b = a+1; b < sum - 2; b++){ c = sum - a - b; if (a*a + b*b == c*c){ printf("a = %i, b = %i, c = %i \n", a, b, c); return 0; } } } printf("no solution found.\n"); return 0; }