List comprehensions: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: changed comments and whitespace, optimized the inner DO loop.) |
|||
Line 1,932: | Line 1,932: | ||
<lang rexx>/*REXX program displays a vertical list of Pythagorean triples up to a specified number.*/ |
<lang rexx>/*REXX program displays a vertical list of Pythagorean triples up to a specified number.*/ |
||
parse arg n . /*obtain optional argument from the CL.*/ |
parse arg n . /*obtain optional argument from the CL.*/ |
||
if n=='' | n=="," then n= |
if n=='' | n=="," then n= 100 /*Not specified? Then use the default.*/ |
||
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; aa=a*a |
||
do b=a+1 to n-1; |
do b=a+1 to n-1; ab=aa + b*b |
||
do c=b+1 to n |
do c=b+1 to n ; cc= c*c |
||
if |
if ab<cc then leave /*Too small? Then try the next B. */ |
||
if ab==cc then do; $=$ '{'a"," || b','c"}"; leave; end |
|||
end /*c*/ |
end /*c*/ |
||
end /*b*/ |
end /*b*/ |
||
end /*a*/ |
end /*a*/ |
||
#=words($) |
#= words($) |
||
do j=1 for # |
do j=1 for # |
||
say left('', 20) word($, j) /*display a member of the list, */ |
say left('', 20) word($, j) /*display a member of the list, */ |
||
end /*j*/ /* [↑] list the members vertically. */ |
end /*j*/ /* [↑] list the members vertically. */ |
||
say |
|||
say # 'members listed.' /*stick a fork in it, we're all done. */</lang> |
say # 'members listed.' /*stick a fork in it, we're all done. */</lang> |
||
{{out|output|text= when using the default input:}} |
{{out|output|text= when using the default input:}} |
||
Line 2,008: | Line 2,010: | ||
<lang rexx>/*REXX program displays a vertical list of Pythagorean triples up to a specified number.*/ |
<lang rexx>/*REXX program displays a vertical list of Pythagorean triples up to a specified number.*/ |
||
parse arg n . /*get the optional argument from the CL*/ |
parse arg n . /*get the optional argument from the CL*/ |
||
if n=='' | n=="," then n= |
if n=='' | n=="," then n= 100 /*Not specified? Then use the default.*/ |
||
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; aa= a*a |
||
do b=a+1 to n-1; |
do b=a+1 to n-1; ab= aa + b*b |
||
do c=b+1 to n |
do c=b+1 to n ; cc= c*c |
||
if |
if ab<cc then leave /*Too small? Then try the next B. */ |
||
if ab==cc 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*/ /*stick a fork in it, we're all done. */ |
||
#=words($) |
#= words($) /*number of members in the list. */ |
||
say; say strip($) /*show the Pythagorean triples to term.*/ |
say; say strip($) /*show the Pythagorean triples to term.*/ |
||
say; say # 'members listed.' /*triples are listed in order of 1st #.*/</lang> |
say; say # 'members listed.' /*triples are listed in order of 1st #.*/</lang> |