Heronian triangles: Difference between revisions

→‎{{header|REXX}}: eliminated the need for a (true) SQRT subroutine, used an integer version (iSQRT) instead; it made the Heronian program four times faster.
m (→‎{{header|REXX}}: optimized program (3 times faster), added more checks for primitive Heronian triangles before calculating SQRT, also, postponed GCD test.)
(→‎{{header|REXX}}: eliminated the need for a (true) SQRT subroutine, used an integer version (iSQRT) instead; it made the Heronian program four times faster.)
Line 671:
 
=={{header|REXX}}==
Programming notes:
Programming note:   the   '''hGCD'''   subroutine is a specialized version of a GCD routine in that it doesn't check for non-positive integers.
 
Programming note:   theThe   '''hGCD'''   subroutine is a specialized version of a GCD routine in that it doesn't check for non-positive integers.
Also, a fair amount of code was added to optimize the speed   (at the expense of program simplicity).
 
Also, a fair amount of code was added to optimize the speed   (at the expense of program simplicity).;   by thoughtful ordering of
<br>the "elimination" checks, and also the use of an integer version of a &nbsp; SQRT &nbsp; subroutine, the execution time was greatly reduced.
 
Note that the &nbsp; '''iSqrt''' &nbsp; subroutine doesn't use floating point.
 
This REXX program doesn't need to explicitly sort the triangles.
Line 696 ⟶ 701:
if pos(.,_)\==0 then iterate /*not an integer. */
parse var _ '' -1 q ; if #.q then iterate /*not good square. */
ar=sqrtIsqrt(_); if pos(.,ar)*ar\==0 _ then iterate /*area not integer.*/
if hGCD(a,b,c)\==1 then iterate /*GCD of sides ¬1. */
#=#+1 /*got prim. H. tri.*/
Line 711 ⟶ 716:
do j=2 for 2; y=arg(j); do until _==0; _=x//y; x=y; y=_; end; end
return x
/*──────────────────────────────────ISQRT subroutine────────────────────*/
sqrtiSqrt: procedure; parse arg x; x=x%1; if x==0 | x==1 then return 0;d=digits()x;numeric digits 11q=1
do while q<=x; q=q*4; end; r=0 /*Q will be > X at loop end.*/
do while q>1 ; q=q%4; _=x-r-q; r=r%2; if _>=0 then do;x=_;r=r+q;end; end
return r /* R is a postive integer. */
/*──────────────────────────────────SHOW subroutine─────────────────────*/
show: m=0; say; say; parse arg ae; say arg(2); if ae\=='' then first=9e9
Line 723 ⟶ 733:
end /*j*/ /* [↑] use known perimeters*/
end /*i*/ /* [↑] show found triangles*/
return</lang>
/*──────────────────────────────────SQRT subroutine─────────────────────────*/
sqrt: procedure; parse arg x;if x=0 then return 0;d=digits();numeric digits 11
numeric form; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g*.5'E'_%2
p=d+d%4+2; m.=11; do j=0 while p>9; m.j=p; p=p%2+1; end; do k=j+5 to 0 by -1
if m.k>11 then numeric digits m.k;g=.5*(g+x/g);end;numeric digits d;return g/1</lang>
'''output'''
<pre>