List comprehensions: Difference between revisions

Content added Content deleted
m (→‎horizontal list: changed a (title) comment.)
m (→‎horizontal list: changed some comments and whitespace,)
Line 2,105: Line 2,105:
===horizontal list===
===horizontal list===
<lang rexx>/*REXX program shows a horizontal list of Pythagorean triples up to a specified number. */
<lang rexx>/*REXX program shows a horizontal list of Pythagorean triples up to a specified number. */
parse arg n . /*get the optional argument from the CL*/
parse arg n . /*obtain optional argument from the CL.*/
if n=='' | n=="," then n= 100 /*Not specified? Then use the default.*/
if n=='' | n=="," then n= 100 /*Not specified? Then use the default.*/
do k=1 for n; @.k= k*k /*precompute the squares of usable #'s.*/
end /*k*/
sw= linesize() - 1 /*obtain the terminal width (less one).*/
say 'Pythagorean triples (a² + b² = c², c ≤' n"):" /*display the list's title. */
say 'Pythagorean triples (a² + b² = c², c ≤' n"):" /*display the list's title. */
$= /*assign a null to the triples list. */
$= /*assign a null to the triples list. */
do a=1 for n-2; aa= a*a
do a=1 for n-2; bump= a//2 /*Note: A*A is faster than A**2. */
do b=a+1 to n-1; ab= aa + b*b
do b=a+1 to n-1 by 1+bump
do c=b+1 to n ; cc= c*c
ab= @.a + @.b /*AB: a shortcut for the sum of A² & B²*/
if ab<cc then leave /*Too small? Then try the next B. */
if bump==0 & b//2==0 then cump= 2
if ab==cc then do; $=$ '{'a"," || b','c"}"; leave; end
else cump= 1
do c=b+cump to n by cump
if ab<@.c then leave /*Too small? Then try the next B. */
if ab==@.c then do; $=$ '{'a"," || b','c"}"; leave; end
end /*c*/
end /*c*/
end /*b*/
end /*b*/
end /*a*/ /*stick a fork in it, we're all done. */
end /*a*/
#= words($)
#= words($) /*number of members in the list. */
say; say strip($) /*show the Pythagorean triples to term.*/
do j=1 until p==0; p= lastPos('}', $, sw) /*find the last } */
if p\==0 then do; _= left($, p)
say; say # 'members listed.' /*triples are listed in order of 1st #.*/</lang>
say strip(_)
$= substr($, p+1)
end
end /*j*/
say strip($)
say
say # ' members listed.' /*stick a fork in it, we're all done. */</lang>
{{out|output|text=&nbsp; when using the following input: &nbsp; <tt> 35 </tt>}}
{{out|output|text=&nbsp; when using the following input: &nbsp; <tt> 35 </tt>}}
<pre>
<pre>