Heronian triangles: Difference between revisions

Content added Content deleted
m (→‎using SQRT table: added whitespace, optimized a DO loop, aligned the END with the DO loop..)
m (→‎using iSQRT: added whitespace, aligned the END with the DO loop.)
Line 3,697: Line 3,697:
<lang rexx>/*REXX program generates & displays primitive Heronian triangles by side length and area*/
<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*/
parse arg N first area . /*obtain optional arguments from the CL*/
if N=='' | N=="," then N=200 /*Not specified? Then use the default.*/
if N=='' | N=="," then N= 200 /*Not specified? Then use the default.*/
if first=='' | first=="," then first= 10 /* " " " " " " */
if first=='' | first=="," then first= 10 /* " " " " " " */
if area=='' | area=="," then area=210 /* " " " " " " */
if area=='' | area=="," then area= 210 /* " " " " " " */
numeric digits 99; numeric digits max(9, 1+length(N**5)) /*ensure 'nuff decimal digits.*/
numeric digits 99 /*ensure 'nuff dec. digs to calc. N**5.*/
numeric digits max(9, 1 + length(N**5) ) /*minimize decimal digits for REXX pgm.*/
call Heron; HT= 'Heronian triangles' /*invoke the Heron subroutine. */
call Heron; HT= 'Heronian triangles' /*invoke the Heron subroutine. */
say # ' primitive' HT "found with sides up to " N ' (inclusive).'
say # ' primitive' HT "found with sides up to " N ' (inclusive).'
Line 3,708: Line 3,709:
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
Heron: @.= 0; minP= 9e9; maxP= 0; maxA= 0; minA= 9e9; Ln= length(N) /* __*/
Heron: @.= 0; minP= 9e9; maxP= 0; maxA= 0; minA= 9e9; Ln= length(N) /* __*/
#=0; #.=0; #.2=1; #.3=1; #.7=1; #.8=1 /*digits ¬good √ */
#= 0; #.= 0; #.2= 1; #.3= 1; #.7= 1; #.8= 1 /*digits ¬good √ */
do a=3 to N /*start at a minimum side length of 3. */
do a=3 to N /*start at a minimum side length of 3. */
Aeven= a//2==0 /*if A is even, B and C must be odd.*/
Aeven= a//2==0 /*if A is even, B and C must be odd.*/
Line 3,727: Line 3,728:
end /*c*/ /* [↑] keep each unique perimeter#*/
end /*c*/ /* [↑] keep each unique perimeter#*/
end /*b*/
end /*b*/
end /*a*/; return # /*return # of Heronian triangles. */
end /*a*/; return # /*return # of Heronian triangles. */
/*─────────────────────────────────────────────────────────────────────────────────────────────*/
hGCD: x=a; 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
hGCD: x=a; do j=2 for 2; y= arg(j); do until y==0; parse value x//y y with y x
q=q%4; _=x-r-q; r=r%2; if _>=0 then parse value _ r+q with x r; end; return r
end /*until*/
end /*j*/; return x
/*──────────────────────────────────────────────────────────────────────────────────────*/
hIsqrt: procedure; parse arg x; q= 1; r= 0; do while q<=x; q= q * 4
end /*while q<=x*/
do while q>1; q=q%4; _= x-r-q; r= r%2; if _>=0 then parse value _ r+q with x r
end /*while q>1*/; return r
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: m=0; say; say; parse arg ae; say arg(2); if ae\=='' then first= 9e9
show: m=0; say; say; parse arg ae; say arg(2); if ae\=='' then first= 9e9