List comprehensions: Difference between revisions
Content added Content deleted
m (→{{header|AppleScript}}: (updated a name in the concat function)) |
m (→{{header|REXX}}: addec/changed some comments and whitespace, changed indentation of some output.) |
||
Line 1,747: | Line 1,747: | ||
<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=='' |
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. */ |
|||
$= /*assign a null to the triples list. */ |
$= /*assign a null to the triples list. */ |
||
do a=1 for n-2; aa=a*a /*Note: A*A is faster than A**2, but */ |
do a=1 for n-2; aa=a*a /*Note: A*A is faster than A**2, but */ |
||
do b=a+1 to n-1; aabb=aa+b*b /* ··· but not by much.*/ |
do b=a+1 to n-1; aabb=aa+b*b /* ··· but not by much.*/ |
||
do c=b+1 to n |
do c=b+1 to n |
||
if aabb==c*c then $=$ ' |
if aabb==c*c then $=$ '{'a"," || b','c"}" |
||
end /*c*/ |
end /*c*/ |
||
end /*b*/ |
end /*b*/ |
||
end /*a*/ |
end /*a*/ |
||
#=words($) |
|||
#=words($) /*number of members in the list. */ |
|||
say 'Pythagorean triples (a² + b² = c², c ≤' n"):" |
|||
say |
|||
do j=1 for # |
do j=1 for # |
||
say word($, j) |
say left('', 20) word($, j) /*display a member of the list, */ |
||
end /*j*/ /* [↑] list the members vertically. */ |
end /*j*/ /* [↑] list the members vertically. */ |
||
say # 'members listed.' /*stick a fork in it, we're all done. */</lang> |
|||
say |
|||
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:}} |
||
<pre style="height: |
<pre style="height:45ex"> |
||
Pythagorean triples |
Pythagorean triples (a² + b² = c², c ≤ 100): |
||
{3,4,5} |
|||
{5,12,13} |
|||
(3,4,5) |
|||
{6,8,10} |
|||
(5,12,13) |
|||
{7,24,25} |
|||
(6,8,10) |
|||
{8,15,17} |
|||
(7,24,25) |
|||
{9,12,15} |
|||
(8,15,17) |
|||
{9,40,41} |
|||
(9,12,15) |
|||
{10,24,26} |
|||
(9,40,41) |
|||
{11,60,61} |
|||
(10,24,26) |
|||
{12,16,20} |
|||
(11,60,61) |
|||
{12,35,37} |
|||
(12,16,20) |
|||
{13,84,85} |
|||
(12,35,37) |
|||
{14,48,50} |
|||
(13,84,85) |
|||
{15,20,25} |
|||
(14,48,50) |
|||
{15,36,39} |
|||
(15,20,25) |
|||
{16,30,34} |
|||
(15,36,39) |
|||
{16,63,65} |
|||
(16,30,34) |
|||
{18,24,30} |
|||
(16,63,65) |
|||
{18,80,82} |
|||
(18,24,30) |
|||
{20,21,29} |
|||
(18,80,82) |
|||
{20,48,52} |
|||
(20,21,29) |
|||
{21,28,35} |
|||
(20,48,52) |
|||
{21,72,75} |
|||
(21,28,35) |
|||
{24,32,40} |
|||
(21,72,75) |
|||
{24,45,51} |
|||
(24,32,40) |
|||
{24,70,74} |
|||
(24,45,51) |
|||
{25,60,65} |
|||
(24,70,74) |
|||
{27,36,45} |
|||
(25,60,65) |
|||
{28,45,53} |
|||
(27,36,45) |
|||
{28,96,100} |
|||
(28,45,53) |
|||
{30,40,50} |
|||
(28,96,100) |
|||
{30,72,78} |
|||
(30,40,50) |
|||
{32,60,68} |
|||
(30,72,78) |
|||
{33,44,55} |
|||
(32,60,68) |
|||
{33,56,65} |
|||
(33,44,55) |
|||
{35,84,91} |
|||
(33,56,65) |
|||
{36,48,60} |
|||
(35,84,91) |
|||
{36,77,85} |
|||
(36,48,60) |
|||
{39,52,65} |
|||
(36,77,85) |
|||
{39,80,89} |
|||
(39,52,65) |
|||
{40,42,58} |
|||
(39,80,89) |
|||
{40,75,85} |
|||
(40,42,58) |
|||
{42,56,70} |
|||
(40,75,85) |
|||
{45,60,75} |
|||
(42,56,70) |
|||
{48,55,73} |
|||
(45,60,75) |
|||
{48,64,80} |
|||
(48,55,73) |
|||
{51,68,85} |
|||
(48,64,80) |
|||
{54,72,90} |
|||
(51,68,85) |
|||
{57,76,95} |
|||
(54,72,90) |
|||
{60,63,87} |
|||
(57,76,95) |
|||
{60,80,100} |
|||
(60,63,87) |
|||
{65,72,97} |
|||
(60,80,100) |
|||
(65,72,97) |
|||
52 members listed. |
52 members listed. |
||
</pre> |
</pre> |
||
Line 1,827: | Line 1,823: | ||
<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=='' |
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. */ |
|||
$= /*assign a null to the triples list. */ |
$= /*assign a null to the triples list. */ |
||
do a=1 for n-2; aa=a*a /*Note: A*A is faster than A**2, but */ |
do a=1 for n-2; aa=a*a /*Note: A*A is faster than A**2, but */ |
||
do b=a+1 to n-1; aabb=aa+b*b /* ··· but not by much.*/ |
do b=a+1 to n-1; aabb=aa+b*b /* ··· but not by much.*/ |
||
do c=b+1 to n |
do c=b+1 to n |
||
if aabb==c*c then $=$ ' |
if aabb==c*c then $=$ '{'a"," || b','c"}" |
||
end /*c*/ |
end /*c*/ |
||
end /*b*/ |
end /*b*/ |
||
end /*a*/ |
end /*a*/ /*stick a fork in it, we're all done. */ |
||
#=words($) /*number of members in the list. */ |
#=words($) /*number of members in the list. */ |
||
say; say strip($) /*show the Pythagorean triples to term.*/ |
|||
say 'Pythagorean triples (a² + b² = c², c ≤' n"):" |
|||
say; say # 'members listed.' /*triples are listed in order of 1st #.*/</lang> |
|||
say |
|||
say strip($) /*show the Pythagorean triples to term.*/ |
|||
say; say # 'members listed.' /*triples are listed in order of 1st #.*/ |
|||
/*stick a fork in it, we're all done. */</lang> |
|||
{{out|output|text= when using the following input: <tt> 35 </tt>}} |
{{out|output|text= when using the following input: <tt> 35 </tt>}} |
||
<pre> |
<pre> |
||
Pythagorean triples |
Pythagorean triples (a² + b² = c², c ≤ 35): |
||
{3,4,5} {5,12,13} {6,8,10} {7,24,25} {8,15,17} {9,12,15} {10,24,26} {12,16,20} {15,20,25} {16,30,34} {18,24,30} {20,21,29} {21,28,35} |
|||
13 members listed. |
13 members listed. |