Pythagorean triples: Difference between revisions

Content added Content deleted
(Added Elixir)
No edit summary
Line 887: Line 887:
Up to 10 ** 10 : 14557915466 triples, 702304875 primitives
Up to 10 ** 10 : 14557915466 triples, 702304875 primitives
Up to 10 ** 11 : 161750315680 triples, 7023049293 primitives
Up to 10 ** 11 : 161750315680 triples, 7023049293 primitives
</pre>

=={{header|ERRE}}==
<lang ERRE>
PROGRAM PIT

BEGIN

PRINT(CHR$(12);) !CLS
PRINT(TIME$)

FOR POWER=1 TO 7 DO
PLIMIT=10#^POWER
UPPERBOUND=INT(1+PLIMIT^0.5)
PRIMITIVES=0
TRIPLES=0
EXTRAS=0 ! will count the in-range multiples of any primitive

FOR M=2 TO UPPERBOUND DO
FOR N=1+(M MOD 2=1) TO M-1 STEP 2 DO
TERM1=2*M*N
TERM2=M*M-N*N
TERM3=M*M+N*N
PERIMETER=TERM1+TERM2+TERM3

IF PERIMETER<=PLIMIT THEN TRIPLES=TRIPLES+1

A=TERM1
B=TERM2

REPEAT
R=A-B*INT(A/B)
A=B
B=R
UNTIL R<=0

! we've found a primitive triple if a = 1, since hcf =1.
! and it is inside perimeter range. Save it in an array
IF (A=1) AND (PERIMETER<=PLIMIT) THEN
PRIMITIVES=PRIMITIVES+1

!-----------------------------------------------
!swap so in increasing order of side length
!-----------------------------------------------
IF TERM1>TERM2 THEN SWAP(TERM1,TERM2)
!-----------------------------------------------
!we have the primitive & removed any multiples.
!Now calculate ALL the multiples in range.
!-----------------------------------------------
NEX=INT(PLIMIT/PERIMETER)
EXTRAS=EXTRAS+NEX
END IF

!scan
END FOR
END FOR

PRINT("Primit. with perimeter <=";10#^power;"is";primitives;"&";extras;"non-prim.triples.")
PRINT(TIME$)
END FOR

PRINT PRINT("** End **")
END PROGRAM</lang>
{{out}}
<pre>16:08:39
Primit. with perimeter <= 10 is 0 & 0 non-prim.triples.
16:08:39
Primit. with perimeter <= 100 is 7 & 17 non-prim.triples.
16:08:39
Primit. with perimeter <= 1000 is 70 & 325 non-prim.triples.
16:08:39
Primit. with perimeter <= 10000 is 703 & 4858 non-prim.triples.
16:08:39
Primit. with perimeter <= 100000 is 7026 & 64741 non-prim.triples.
16:08:41
Primit. with perimeter <= 1000000 is 70229 & 808950 non-prim.triples.
16:09:07
.......................
</pre>
</pre>