Anonymous user
Pythagorean triples: Difference between revisions
→{{header|C}}: semi-optimized code
m (wordliness) |
(→{{header|C}}: semi-optimized code) |
||
Line 13:
* [[List comprehensions]]
=={{header|C}}==
Sample implemention; naive method,
<lang C>#include <stdio.h>
#include <stdlib.h>
typedef unsigned long long xint;
typedef unsigned long ulong;
{
while (n) { t = n; n = m % n; m = t; }
return m;
}
int main()
{
for (a = 1; a < 100; a++) {▼
for (b = a; b < 100; b++) {▼
for (c = b; c < 100; c++) {▼
if (a + b + c > 100) break;▼
pytha++;▼
printf("a = %lu\r", a); /* show that we are working */
fflush(stdout);
/* max_p/2: valid limit, because one side of triangle
* must be less than the sum of the other two
*/
bb = (xint)b * b;
cc = (xint)c * c;
if (aa + bb < cc) break;
if (aa + bb == cc) {
▲ pytha++;
if (gcd(a, b) == 1) prim++;
}
}
}
}
max_p, pytha, prim);▼
▲ printf("Up to 100, there are %d triples, of which %d are primitive\n",
▲ pytha, prim);
return 0;
}</lang>output:<lang>Up to 100, there are 17 triples, of which 7 are primitive</lang>
=={{header|Java}}==
[[Category:Arbitrary precision]]Theoretically, this can go "forever", but it takes a while, so only the minimum is shown. Luckily, <code>BigInteger</code> has a GCD method built in.
|