[M3devel] LONGINT problem?

Ken Durocher kcdurocher at gmail.com
Thu Jul 14 23:26:05 CEST 2011


I was writing a program to calculate "Pythagorean triples" recursively, and
ran into a problem.  Here's the program:

MODULE PyTriples EXPORTS Main;

IMPORT IO, Fmt;

VAR tcnt, pcnt, max, i: LONGINT;

PROCEDURE NewTriangle(a, b, c: LONGINT; VAR tcount, pcount: LONGINT) =
  VAR perim := a + b + c;
  BEGIN
    IF perim <= max THEN
      pcount := pcount + 1L;
      tcount := tcount + max DIV perim;
      NewTriangle(a-2L*b+2L*c, 2L*a-b+2L*c, 2L*a-2L*b+3L*c, tcount, pcount);
      NewTriangle(a+2L*b+2L*c, 2L*a+b+2L*c, 2L*a+2L*b+3L*c, tcount, pcount);
      NewTriangle(2L*b+2L*c-a, b+2L*c-2L*a, 2L*b+3L*c-2L*a, tcount, pcount);
    END;
  END NewTriangle;

BEGIN
i := 100L;

REPEAT
  max := i;
  tcnt := 0L;
  pcnt := 0L;
  NewTriangle(3L, 4L, 5L, tcnt, pcnt);
  IO.Put(Fmt.LongInt(i) & ": " & Fmt.LongInt(tcnt) & " Triples, " &
    Fmt.LongInt(pcnt) & " Primitives\n");
  i := i * 10L;
UNTIL i = 10000000L;

END PyTriples.

This outputs:

100: 17 Triples, 7 Primitives
1000: 325 Triples, 70 Primitives
10000: 0858 Triples, 703 Primitives
100000: 40701 Triples, 7024 Primitives
1000000: 808950 Triples, 70229 Primitives

However, if I just use INTEGER on a 64 bit machine, I get the proper output:

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

Note how 10000 and 100000 are different. The code is literally exactly the
same, only with LONGINT replaced by INTEGER.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://m3lists.elegosoft.com/pipermail/m3devel/attachments/20110714/1b71d4d1/attachment-0001.html>


More information about the M3devel mailing list