Anonymous user
Law of cosines - triples: Difference between revisions
→{{header|REXX}}: optimized DO loops (using FOR), implemented the use of commas in large numbers, moved each set of computation to subroutines, added memoizations, changed whitespace and comments.
Alextretyak (talk | contribs) (Added 11l) |
(→{{header|REXX}}: optimized DO loops (using FOR), implemented the use of commas in large numbers, moved each set of computation to subroutines, added memoizations, changed whitespace and comments.) |
||
Line 1,844:
=={{header|REXX}}==
Instead of coding a general purpose subroutine (or function) to solve all of the
task's requirements, it was decided to
Line 1,863 ⟶ 1,864:
<br>absolute value of the negative number).
<lang rexx>/*REXX pgm finds integer sided triangles that satisfy Law of cosines for 60º, 90º, 120º.*/
parse arg
if
if
if
if os4=='' | os4=="," then os4= -0; s4=abs(os4) /* " " " " " " */
@.= /*@: array holds squares, max of sides*/
end
if s1>0 then call s1 /*handle the triangle
if s2>0 then call s2 /*handle the triangle case for
if s3>0 then call s3
if s4>0 then call s4 /*handle the case for unique sides.
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
head: #= 0; parse arg d,uq,s; @= ','; call dAng; say center(ang, 65, '═'); return
show: #=#+1; arg p; if p>0 then say ' ('right(a,w)@ right(b,w)@ right(c,w)")"; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
s1: call head 120,,s1 /*────────── 120º: a² + b² + ab ≡ c² */
do a=1 for s1; ap1= a + 1
do b=ap1 for s1-ap1+1; x= @.a + @.b + a*b; if x>ss then iterate a
do c=b+1 for s1-b+1 until @.c>x
if x==@.c then do; call show os1; iterate b; end
end /*c*/
end /*b*/
end /*a*/
call foot s1; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
s2: call head 90,,s2 /*────────── 90º: a² + b² ≡ c² */
do a=1 for s2; ap1= a + 1
do b=ap1 for s2-ap1+1; x= @.a + @.b; if x>ss then iterate a
do c=b+1 for s2-b+2 until @.c>x
if x==@.c then do; call show os2; iterate b; end
end /*c*/
end /*b*/
end /*a*/
call foot s2; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
s3: call head 60,,s3 /*────────── 60º: a² + b² ─ ab ≡ c² */
do a=1 for s3; s3ma= s3 - a + 1
do b=a for s3ma; x= @.a + @.b - a*b; if x>ss then iterate a
do c=a for s3ma until @.c>x
if x==@.c then do; call show os3; iterate b; end
end /*c*/
end /*b*/
end /*a*/
call foot s2; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
s4: call head 60, 'unique', os4 /*────────── 60º: a² + b² ─ ab ≡ c² */
do a=1 for s4; ap1= a + 1; s4map1= s4 - ap1 + 1
do b=ap1 for s4map1; x= @.a + @.b - a*b; if x>ss then iterate a
do c=ap1 for s4map1 until @.c>x
if x==@.c then do; call show os4; iterate b; end
end /*c*/
end /*b*/
end /*a*/
call foot s4; return</lang>
{{out|output|text= when using the default number of sides for the input: <tt> 13 </tt>}}
<pre>
Line 1,937 ⟶ 1,954:
15 solutions found for 60º (sides up to 13)
</pre>
{{out|output|text= when using the inputs of: <tt> 0 0 0 -10000 </tt>}}
Note that the first three computations are bypassed because of the three zero ('''0''') numbers, the negative ten thousand indicates to find all the triangles with sides up to 10,000, but not list the triangles, it just reports the ''number'' of
<pre>
══════════════════════════ 60º unique═══════════════════════════
</pre>
|