Jump to content

Loops/With multiple ranges: Difference between revisions

Line 1,582:
 
=={{header|J}}==
J uses the names x, y, m, n, u, v to pass arguments into explicit definitions. Treating these as reserved names is reasonable practice. Originally these had been x. , y. etceteras however the dots must have been deemed "noisy".
 
Rendered in ALL CAPS, because this kind of code ought to be in ALL CAPS.
We've passed the range list argument literally for evaluation in local scope. Verb f evaluates and concatenates the ranges, then perhaps the ensuing for. loop looks somewhat like familiar code.
 
<syntaxhighlight lang="j"J>TO=: {{
'N M'=. 2{.y,1
NB. http://rosettacode.org/wiki/Loops/Wrong_ranges#J
x:x+M*i.>.(1+N-x)%M
NB. define range as a linear polynomial
}}
start =: 0&{
stop =: 1&{
increment =: 2&{ :: 1: NB. on error use 1
range =: (start , increment) p. [: i. [: >: [: <. (stop - start) % increment
 
f BY=: 3 :0,
 
input =. y
{{
'prod sum x y z one three seven' =. 1 0 5 _5 _2 1 3 7
PROD=: 1
J =. ([: ; range&.>) ". input
SUM=: 0
for_j. J do.
sum X=. sum: + | j5
Y=: -5
if. ((|prod)<2^27) *. (0 ~: j) do.
prod Z=. prod *: j-2
ONE=: 1
end.
THREE=: 3
SEVEN=: 7
 
for_J. ;do >cutLF {{)n
< (-THREE) TO (3^3) BY THREE
< (-SEVEN) TO (+SEVEN) BY X
< 555 TO 550-Y
< 22 TO 348173 _28 _793618560 BY -THREE
< 1927 TO 1939
< X TO Y BY Z
< (11^X) TO (11^X) + ONE
}} do.
SUM=: SUM+|J
if. ((|prodPROD)<2^27) *. (J~:0 ~do. PROD=: j)PROD*J doend.
end.
echo ' SUM= ',":SUM
sum , prod
echo 'PROD= ',":PROD
)
}}0</syntaxhighlight>
 
Running this script produces this output:
 
<pre> SUM= 348168
PROD= _793618560</pre>
] A =: f '((-three), (3^3), three); ((-seven),seven,x); (555 , 550-y); (22 _28, -three); 1927 1939; (x,y,z); (0 1 + 11^x)'
348173 _7.93619e8
20j0 ": A
348173 _793618560
</pre>
 
=={{header|Java}}==
6,962

edits

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