Jump to content

Loops/With multiple ranges: Difference between revisions

m
→‎{{header|Perl 6}}: Combine code blocks to make complete, runnable examples, twiddle whitespace
(Flagged for clarification.)
m (→‎{{header|Perl 6}}: Combine code blocks to make complete, runnable examples, twiddle whitespace)
Line 288:
Also displaying the j sequence since it isn't very large.
 
<lang perl6>sub comma ($i) {
my $sign = $i < 0 ?? '-' !! '';
$sign ~ $i.abs.flip.comb(3).join(',').flip
Line 301:
 
my $j = flat
( -three, *+three … ),
( -seven, *+x …^ * > seven ),
( 555 .. 550 - y ),
( 22, *-three …^ * < -28 ),
( 1927 .. 1939 ),
( x, *+z …^ * < y ),
( 11**x .. 11**x + one );
 
put 'j sequence: ', $j;
put ' Sum: ', comma [+] $j».abs;
put ' Product: ', comma ([\*] $j.grep: so +*).first: *.abs > 2²⁷;</lang>
 
{{Out}}
An# Or, an alternate method for generating the 'j' sequence, employing user-defined operators to preserve the 'X to Y by Z' layout of the example code.
<pre>j sequence: -3 0 3 6 9 12 15 18 21 24 27 -7 -2 3 555 22 19 16 13 10 7 4 1 -2 -5 -8 -11 -14 -17 -20 -23 -26 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 5 3 1 -1 -3 -5 161051 161052
# operators to more closely mimic the 'X to Y by Z' layout of the example code.
Sum: 348,173
 
Product: -793,618,560</pre>
<lang perl6>sub infix:<to> { $^a...$^b }
==== Literal-minded variation ====
An alternate method for generating the 'j' sequence, employing user-defined operators to preserve the 'X to Y by Z' layout of the example code.
<lang perl6>sub infix:<to> { $^a...$^b }
sub infix:<by> { ($^a.split(' '))[0,$^b.abs ... *] }
 
$j = cache flat
-three to 3**3 by three ,
-seven to seven by x ,
555 to (550 - y) ,
Line 328 ⟶ 326:
1927 to 1939 by one ,
x to y by z ,
11**x to (11**x + one);</lang>
 
put "\nLiteral minded variant:";
put ' Sum: ', comma [+] $j».abs;
put ' Product: ', comma ([\*] $j.grep: so +*).first: *.abs > 2²⁷;</lang>
{{Out}}
<pre>j sequence: -3 0 3 6 9 12 15 18 21 24 27 -7 -2 3 555 22 19 16 13 10 7 4 1 -2 -5 -8 -11 -14 -17 -20 -23 -26 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 5 3 1 -1 -3 -5 161051 161052
Sum: 348,173
Product: -793,618,560</pre>
 
Literal minded variant:
Sum: 348,173
Product: -793,618,560
</pre>
 
=={{header|REXX}}==
10,339

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.