Heronian triangles: Difference between revisions

m
→‎{{header|REXX}}: optimized program (3 times faster), added more checks for primitive Heronian triangles before calculating SQRT, also, postponed GCD test.
(added ooRexx)
m (→‎{{header|REXX}}: optimized program (3 times faster), added more checks for primitive Heronian triangles before calculating SQRT, also, postponed GCD test.)
Line 682:
if area=='' | area==',' then area=210 /* " " " " */
numeric digits max(9, 1+length(N**4)) /*ensure enough decimal digs*/
call Heron /*invoke Heron subroutine. */
@pht=' primitive Heronian triangles' /*define handy-dandy literal*/
callsay Heronian # ' primitive Heronian triangles found with sides up to ' N " /*invoke Heronian subroutine*/(inclusive)."
call show , 'listing of the first ' first ' primitive Heronian triangles:'
say # @pht 'found with sides up to ' N " (inclusive)."
call show area, 'listing of the first(above) 'found primitive Heronian firsttriangles with an area of ' @pht":"area
call show area, 'listing of the (above) found'@pht "with an area of " area
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────HERON subroutine────────────────────*/
/*──────────────────────────────────HERONIAN subroutine─────────────────*/
HeronianHeron: @.=.; y=' #=0; '; minP=9e9; maxP=0; minA=9e9; maxA=0; Ln=length(N)
do a=3 to N #=0; #.=0; #.2=1 #.3=1; #.7=1; #.8=1 /*start at minimum side¬good length.*/
do do b=a=3 to N; ab=a+b /*AB:start at is useda forminimum summingside belowlength.*/
do do cb=ba to N; ab=a+b /*AB: [↓] is ifused GCD ¬=1, it's not afor summing below.*/
ifdo hGCD(a,b,c)\=b to N; p=1ab+c; then iterates=p/2 /*calculate the Perimeter primitiveand Heronian triangleS.*/
p_=ab+s*(s-a)*(s-b)*(s-c ); if s_<=p*.50 then iterate /*calculate the perimeter and_ isn't Spositive.*/
_=(s-a)*(s-b)*(s-c); if pos(.,_<)\==0 then iterate /*¬positivenot an integer. */
ar=sqrt(s*_)parse var _ '' -1 q ; if pos(#.,ar)\==0q then iterate /*area¬intnot good square. */
ar=sqrt(_); if pos(.,ar)\==0 then iterate /*area=zero not integer.*/
#if hGCD(a,b,c)\==#+1 then iterate /*gotGCD primitiveof Heroniansides ¬1. triangle*/
#=#+1 /*got prim. H. tri.*/
minP=min( p,minP); maxP=max( p,maxP); Lp=length(maxP)
minAminP=min(ar p,minAminP); maxAmaxP=max(ar p,maxAmaxP); LaLp=length(maxAmaxP); @.ar=
minPminA=min( par,minPminA); maxPmaxA=max( par,maxPmaxA); LpLa=length(maxPmaxA); @.ar=
if @.ar.p.0==. then @.ar.p.0=0; _=@.ar.p.0+1 /*bump triangle ctr*/
@.ar.p.0=_; @.ar.p._=right(a,Ln) right(b,Ln) right(c ,Ln) /*unique triangle. */
end /*c*/ /* [↑] keep each unique P items.*/
end /*b*/
Line 708:
return # /*return # of Heronian triangles.*/
/*──────────────────────────────────HGCD subroutine─────────────────────*/
hGCD: procedure; parse arg x /*this sub handles exactly 3 args*/
do j=2 for 2; y=arg(j); do until _==0; _=x//y; x=y; y=_; end; end
return x
/*──────────────────────────────────SHOW subroutine─────────────────────*/
show: m=0; say; say; parse arg ae,txt; say txtarg(2); if ae\=='' then first=9e9
say /* [↓] skip the nothings. */
say
do i=minA to maxA; if @.i==. then iterate /*skip nulls.*/
if ae\=='' & i\==ae then iterate /*Area specified? Then check*/
do j=minP to maxP until m>=first /*only list FIRST entries.*/
if @.i.j.0==. then iterate /*Not defined? Then skip it.*/
do k=1 for @.i.j.0; m=m+1 /*visit each perimeter entry*/
say right(m,9) y' area:' right(i,La) y" perimeter:" right(j,Lp) ,y'sides:' @.i.j.k
m=m+1; parse var @.i.j.k a b c /*get sides for nice display*/
say right(m,9) ' area:' right(i,La) " perimeter:" ,
right(j,Lp) ' sides:' right(a,Ln) right(b,Ln) right(c,Ln)
end /*k*/
end /*j*/ /* [↑] use known perimeters*/
end /*i*/ /* [↑] show allfound triangles. */
return
/*──────────────────────────────────SQRT subroutine─────────────────────────*/
Line 738 ⟶ 736:
listing of the first 10 primitive Heronian triangles:
 
1 area: 6 perimeter: 12 sides: 3 4 5
2 area: 12 perimeter: 16 sides: 5 5 6
3 area: 12 perimeter: 18 sides: 5 5 8
4 area: 24 perimeter: 32 sides: 4 13 15
5 area: 30 perimeter: 30 sides: 5 12 13
6 area: 36 perimeter: 36 sides: 9 10 17
7 area: 36 perimeter: 54 sides: 3 25 26
8 area: 42 perimeter: 42 sides: 7 15 20
9 area: 60 perimeter: 36 sides: 10 13 13
10 area: 60 perimeter: 40 sides: 8 15 17
 
 
listing of the (above) found primitive Heronian triangles with an area of 210
 
1 area: 210 perimeter: 70 sides: 17 25 28
2 area: 210 perimeter: 70 sides: 20 21 29
3 area: 210 perimeter: 84 sides: 12 35 37
4 area: 210 perimeter: 84 sides: 17 28 39
5 area: 210 perimeter: 140 sides: 7 65 68
6 area: 210 perimeter: 300 sides: 3 148 149
</pre>