Heronian triangles: Difference between revisions
Content deleted Content added
Added Factor |
m →{{header|REXX}}: simplified some statements, added/changed comments and whitespace, optimized both REXX versions.. |
||
Line 3,708:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
Heron: @.= 0; minP= 9e9; maxP= 0; maxA= 0; minA= 9e9; Ln= length(N) /*
do
do
if b//2==0 then
else
do c=b+bump to N by 2; s= (ab + c) % 2
_= s*(s-a)*(s-b)*(s-c);
parse var _ '' -1 z
ar= hIsqrt(_);
if hGCD(a, b, c) \==
#= # + 1;
minP= min( p, minP); maxP= max( p, maxP); Lp= length(maxP)
minA= min(ar, minA); maxA= max(ar, maxA); La= length(maxA)
_=@.ar.p.0 + 1 /*bump Heronian triangle counter. */
@.ar.p.0= _; @.ar.p._= right(a, Ln) right(b, Ln) right(c, Ln)
end /*c*/ /* [↑] keep each unique perimeter#*/
end /*b*/
/*─────────────────────────────────────────────────────────────────────────────────────────────*/
▲return # /*return number of Heronian triangles. */
hGCD:
▲hGCD: procedure; parse arg x; do j=2 for 2; y=arg(j); do until y==0; parse value x//y y with y x; end; end; return x
/*──────────────────────────────────────────────────────────────────────────────────────*/
hIsqrt: procedure; parse arg x; q=1; r=0; do while q<=x; q=q*4; end; do while q>1
q=q%4; _=x-r-q; r=r%2; if _>=0 then parse value _ r+q with x r; end; return r
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: m=0; say; say; parse arg ae; say arg(2); if ae\=='' then first= 9e9
say; $=left('',9); $a= $"area:"; $p= $'perimeter:';
do i=minA to maxA; if ae\=='' & i\==ae then iterate /*= area? */
do j=minP to maxP until m>=first /*only display the FIRST entries.*/
do k=1 for @.i.j.0; m= m +
say right(m, 9)
end /*k*/
end /*j*/ /* [↑] use the known perimeters. */
end /*i*/;
{{out|output|text= when using the default inputs:}}
<pre>
Line 3,782 ⟶ 3,780:
<lang rexx>/*REXX program generates & displays primitive Heronian triangles by side length and area*/
parse arg N first area . /*obtain optional arguments from the CL*/
if N=='' | N=="," then N=
if first=='' | first=="," then first=
if area=='' | area=="," then area=
numeric digits 99; numeric digits max(9, 1+length(N**5)) /*ensure 'nuff decimal digits.*/
call Heron; HT= 'Heronian triangles' /*invoke the Heron subroutine. */
Line 3,792 ⟶ 3,790:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
Heron: @.= 0; #= 0; !.= .; minP= 9e9; maxA= 0; maxP= 0; minA= 9e9; Ln= length(N)
if b//2==0 then
else if Aeven then bump= 0 /*Is A even?
if hGCD(a,b,c) \== 1 then iterate
#= # + 1; p= ab + c; ar=
minP= min( p, minP); maxP= max( p, maxP); Lp= length(maxP)
minA= min(ar, minA); maxA= max(ar, maxA); La= length(maxA); @.ar=
_= @.ar.p.0 +
@.ar.p.0= _; @.ar.p._= right(a, Ln) right(b, Ln) right(c, Ln)
end /*c*/ /* [↑] keep each unique perimeter #. */
end /*b*/
/*─────────────────────────────────────────────────────────────────────────────────────────────*/
▲return # /*return number of Heronian triangles. */
hGCD:
▲hGCD: procedure; parse arg x; do j=2 for 2; y=arg(j); do until y==0; parse value x//y y with y x; end; end; return x
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: m=0; say; say; parse arg ae; say arg(2);
say; $=left('',9); $a= $"area:"; $p= $'perimeter:';
do i=minA to maxA; if ae\=='' & i\==ae then iterate /*= area? */
do j=minP to maxP until m>=first /*only display the FIRST entries.*/
do k=1 for @.i.j.0; m= m +
say right(m, 9)
end /*k*/
end /*j*/ /* [↑] use the known perimeters. */
end /*i*/;
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}} <br><br>
|