List comprehensions: Difference between revisions

m
→‎horizontal list: changed some comments and whitespace,
m (→‎horizontal list: changed a (title) comment.)
m (→‎horizontal list: changed some comments and whitespace,)
Line 2,105:
===horizontal list===
<lang rexx>/*REXX program shows a horizontal list of Pythagorean triples up to a specified number. */
parse arg n . /*get theobtain optional argument from the CL.*/
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= wordslinesize($) - 1 /*numberobtain ofthe membersterminal inwidth the(less listone). */
say 'Pythagorean triples (a² + b² = c², c ≤' n"):" /*display the list's title. */
$= /*assign a null to the triples list. */
do a=1 for n-2; aa bump= a//2 /*aNote: A*A is faster than A**2. */
do b=a+1 to n-1; ab= aaby 1+ b*bbump
ab= @.a do+ c=@.b+1 to n ; cc= c /*AB: a shortcut for the sum of A² & B²*c/
if ab<cc then leave bump==0 & b/*Too/2==0 small? then cump= Then try the next B. */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 /*b*/
end /*a*/ /*stick a fork in it, we're all done. */
#= words($)
#= words($) /*number of members in the list. */
say; say strip($) do j=1 until p==0; p= lastPos('}', $, sw) /*showfind the Pythagorean tripleslast to} term.*/
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>}}
<pre>