Anonymous user
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.▼
▲
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)
<br>the "elimination" checks, and also the use of an integer version of a SQRT subroutine, the execution time was greatly reduced.
Note that the '''iSqrt''' 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=
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────────────────────*/
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: procedure; parse arg x;if x=0 then return 0;d=digits();numeric digits 11
'''output'''
<pre>
|