/* Der Foist C program made by Matt Chisholm. September 1,1996.*/ /* Finds integer solutions to a^2 + b^2 = c^2 (Mr. Pythagoras' idea.) */ /* Sept. 2 reworked variables and structure */ /* Sept. 3 added checking for duplicate solutions */ /* Sept. 4 figured a better way to check for duplicates */ /* " " added user definable limit for a, b must be less than 127 on a mac*/ /* Oct. 1 added checking for multiples - now displays only prime solutions */ /* March 8, 1997- removed "=" in line 54- now displays all prime solutions, */ /* previously stopped around #26, because would check sol'n against itself*/ /* thanks Toshio! */ /* March 15 added checker for limit being above 1979- numbers get too big */ /* for stinky ol' SunOS */ #if defined(__STDC__) && !defined(__HIGHC__) #include #endif #include #include #define LIMIT 100 main(void){ long int a = 1; /* a side */ long int b = 1; /* b side */ long int x; /* a ^ 2 */ long int y; /* b ^ 2 */ long int z; /* x + y */ long double r; /* root(z) (hypotenuse) */ long int c; /* least greater integer of r */ long int d[LIMIT]; /* first array for all a */ long int e[LIMIT]; /* first array for all b */ long int f[LIMIT]; /* first array for all c */ long int l; /* limit for a, b */ long int m = 0; /* array ramp for removing non primes */ long int n = 0; /* master array counter */ long double o; /* ratio between a's */ long double p; /* ratio between b's */ long int k = 0; /* if one then prime */ long double s,t; /* doubles for dividing with */ printf("Enter limit> "); /* get limit for a, b */ scanf("%d", &l); if (l > 1979) { /*limit on SunOS */ printf("%d is too large. Pick a limit less than 1,980\n", l); a = l + 1; } while (a <= l){ /* less than limit - finds combinations */ while (b <= l){ k = 0; /* Assume that the next solution is not a prime */ x = a * a; /* square a */ y = b * b; /* square b */ z = x + y; /* a^2 + b^2 is z */ r = sqrt(z); /* root of z is r */ c = ceil(r); /* greater closest integer to r */ if (c == r) { /* if they are same, is a triple */ k = 1; /* let's hope it is a prime */ m = 0; /* start at the beginning of the array */ while (m < n) { s = d[m]; /* convert ints to doubles, divide them */ t = a; o = (s)/(t); s = e[m]; /* convert ints to doubles, divide them */ t = b; p = (s)/(t); if (o == p) { /* if they're the same, then it's a multiple */ m = n; /* to exit the loop */ k = 0; /* flag not a prime */ } m = m +1; /* move on to the next set in the array */ } if (k == 1) { /* if flag still says yes, it's a prime */ d[n] = a; /* set them into the array */ e[n] = b; f[n] = c; n = n + 1; /* next number in the array */ } } b = b + 1; /* try next b */ } a = a + 1; /* try next a */ b = a; /* set b to a so that a is always greater-removes duplicate solutions */ } m = 0; while ( m < n ) { printf("#%d: ", m); printf("%d, ", d[m] ); printf("%d, ", e[m] ); printf("%d\n", f[m] ); m = m + 1; } }