Heronian triangles: Difference between revisions

Content deleted Content added
Walterpachl (talk | contribs)
m typo 2
m →‎{{header|REXX}}: changed/added some comments and whitespace, resolved cut&paste error, clarified what is being displayed.
Line 508:
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).
 
This REXX program doesn't need to explicitly sort the triangles.
<lang rexx>/*REXX pgm generates primitive Heronian triangles by side length & area.*/
parse arg N first area . . /*get optional N (sides). */
if N=='' then| N=200 =',' then N=200 /*maybe use the default. */
sidesif first==N'' | first==',' then first= 10 /* " " " " /* 05 Jan 2015 added by WP */
if first area=='' then| first area= 10 =',' then area=210 /* " " " " */
numeric digits max(9, 1+length(sidesN**4)) /*ensure enough dec digits.decimal digs*/
if area=='' then area=210 /* " " " " */
@pht=' primitive Heronian triangles' /*define handy-dandy literal*/
numeric digits max(9, 1+length(sides**4)) /*ensure enough dec digits. */
@pmt=' primitivecall Heronian triangles' /*defineinvoke handy-dandyHeronian literalsubroutine*/
say # @pmtpht 'found with sides up to ' sides N " (inclusive)."
call heronian sides /*invoke sub with max SIDES.*/
call show area , 'listing of the first '@pmt "with area="first area @pht":"
say # @pmt 'found with sides up to ' sides " (inclusive)."
call show area, 'listing of the first(above) found'@pht "with an firstarea of " @pmt":"area
call show area, 'listing of the'@pmt "with area=" area
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────HERONIAN subroutine─────────────────*/
heronianHeronian: @.=.; #=0; minP=9e9; maxP=0; minA=9e9; maxA=0; Ln=length(N)
do a=3 to N /*start at minimum side length.*/
do b=a to N; ab=a+b /*AB: is used for summing below.*/
do c=b to N /* [↓] if GCD ¬=1, it's not a */
if hGCD(a,b,c)\==1 then iterate /* primitive Heronian triangle.*/
Line 532 ⟶ 533:
ar=sqrt(s*_) ; if pos(.,ar)\==0 then iterate /*area¬int.*/
if ar==0 then iterate /*area=zero*/
#=#+1 /*agot primitive Heronian triangle. */
minP=min( p,minP); maxP=max( p,maxP); Lp=length(maxP)
minA=min(ar,minA); maxA=max(ar,maxA); La=length(maxA); @.ar=
if @.ar.p.0==. then @.ar.p.0=0; _=@.ar.p.0+1 /*bump triangle ctr*/
@.ar.p.0=_; @.ar.p._=a b c /*unique triangle. */
end /*c*/ /* [↑] keep each unique P items.*/
end /*b*/
end /*a*/
 
return # /*return # of Heronian triangles.*/
/*──────────────────────────────────HGCD subroutine─────────────────────*/
hGCD: procedure; parse arg x /*this sub handles exactly 2 args*/
do j=2 for 2; y=arg(j); do until _==0; _=x//y; x=y; y=_; end; end
return x
Line 551:
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 /*loop throughvisit each element.perimeter entry*/
m=m+1; parse var @.i.j.k a b c /*parseget sides for nice formatting.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*/
end /*i*/ /*i [↑] show all triangles. */
return
/*──────────────────────────────────SQRT subroutine─────────────────────────*/
Line 585:
 
 
listing of the (above) found primitive Heronian triangles with an area= of 210
 
1 area: 210 perimeter: 70 sides: 17 25 28