Jump to content

Pythagorean triples: Difference between revisions

Add 64bit only Modula-3
m (→‎{{header|Perl 6}}: Tweak brute-force example a bit)
(Add 64bit only Modula-3)
Line 457:
1000000: 808950 triples, 70229 primitive.
10000000: 9706567 triples, 702309 primitive.</pre>
 
=={{header|Modula-3}}==
Note that this code only works on 64bit machines (where <tt>INTEGER</tt> is 64 bits). Modula-3 provides a <tt>LONGINT</tt> type, which is 64 bits on 32 bit systems, but there is a bug in the implementation apparently.
<lang modula3>MODULE PyTriple64 EXPORTS Main;
 
IMPORT IO, Fmt;
 
VAR tcnt, pcnt, max, i: INTEGER;
 
PROCEDURE NewTriangle(a, b, c: INTEGER; VAR tcount, pcount: INTEGER) =
VAR perim := a + b + c;
BEGIN
IF perim <= max THEN
pcount := pcount + 1;
tcount := tcount + max DIV perim;
NewTriangle(a-2*b+2*c, 2*a-b+2*c, 2*a-2*b+3*c, tcount, pcount);
NewTriangle(a+2*b+2*c, 2*a+b+2*c, 2*a+2*b+3*c, tcount, pcount);
NewTriangle(2*b+2*c-a, b+2*c-2*a, 2*b+3*c-2*a, tcount, pcount);
END;
END NewTriangle;
 
BEGIN
i := 100;
REPEAT
max := i;
tcnt := 0;
pcnt := 0;
NewTriangle(3, 4, 5, tcnt, pcnt);
IO.Put(Fmt.Int(i) & ": " & Fmt.Int(tcnt) & " Triples, " &
Fmt.Int(pcnt) & " Primitives\n");
i := i * 10;
UNTIL i = 10000000;
END PyTriple64.</lang>
 
Output:
<pre>
100: 17 Triples, 7 Primitives
1000: 325 Triples, 70 Primitives
10000: 4858 Triples, 703 Primitives
100000: 64741 Triples, 7026 Primitives
1000000: 808950 Triples, 70229 Primitives
</pre>
 
=={{header|Perl 6}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.