Loops/With multiple ranges: Difference between revisions

m
(→‎{{header|TXR}}: New section.)
m (→‎{{header|Wren}}: Minor tidy)
 
(13 intermediate revisions by 9 users not shown)
Line 523:
prod= -793618560
</pre>
 
=={{header|Amazing Hopper}}==
<syntaxhighlight lang="c">
#include <jambo.h>
 
Main
prod=1, sum=0, x=5, y=-5,z=-2,one=1, three=3, seven=7, j=0
Set decimal '0'
Let ' j := Neg(three) '
Iterator( j+=three, Less equal( j, Pow(3,3) ), Set 'j'; Gosub(Body) )
Let ' j := Neg(seven) '
Iterator( j+=x, Less equal( j, seven ), Set 'j'; Gosub(Body) )
j=555, Iterator( ++j, Less equal( j, Add(550,y) ), Set 'j'; Gosub(Body) )
j=22, Iterator( j-=three, Greater equal( j, -28 ), Set 'j'; Gosub(Body) )
j=x, Iterator( j+=z, Greater equal( j, y ), Set 'j'; Gosub(Body) )
j=1927, Iterator( ++j, Less equal( j, 1939 ), Set 'j'; Gosub(Body) )
Let ' j := Pow(11,x)'
Iterator( ++j, Less equal( j, Add( Pow(11,x),one) ), Set 'j'; Gosub(Body) )
 
Printnl ( "SUM = ", sum, "\nPROD = ",prod )
End
 
Subrutines
 
Define ' Body, j '
Let( sum := Add(sum, Abs(j) ) )
When ( Less ( Abs(prod), Pow(2,27) ) And (Not zero(j)) ) {
Let( prod := Mul(prod, j) )
}
Return
</syntaxhighlight>
{{out}}
<pre>
SUM = 348173
PROD = -793618560
</pre>
 
=={{header|Applesoft BASIC}}==
All of the numeric variables are Floating Point but get displayed as integers. The variable ONE is named UNO because ON is a keyword.
Line 763 ⟶ 801:
-793618560
</pre>
 
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">multiLoop: function [ranges, it, blk][
loop ranges 'rng [
loop rng 'r [
let it r
do blk
]
]
]
 
x: 5
y: neg 5
z: neg 2
one: 1
three: 3
seven: 7
 
totalSum: 0
totalProduct: 1
 
multiLoop @[
range.step:three neg three 3^3
range.step:x neg seven seven
range 555 550-y
range.step:neg three 22 neg 28
range 1927 1939
range.step: z x y
range 11^x 1+11^x
] 'i [
totalSum: totalSum + abs i
if and? (abs totalProduct) < 2^27
i <> 0 ->
totalProduct: totalProduct * i
]
 
print ["Sum:" totalSum]
print ["Product:" totalProduct]</syntaxhighlight>
 
{{out}}
 
<pre>Sum: 348173
Product: -793618560</pre>
 
=={{header|AutoHotkey}}==
Line 804 ⟶ 885:
<pre>sum = 348,173
prod = -793,618,560</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
Line 1,015 ⟶ 1,097:
<pre>sum = 348173
prod = -793618560</pre>
 
=={{header|Chipmunk Basic}}==
{{works with|Chipmunk Basic|3.6.4}}
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">100 cls
110 prod = 1 : sum = 0
120 x = 5 : y = -5 : z = -2
130 uno = 1 : tres = 3 : siete = 7
140 for j = -tres to (3^3) step tres : process(j) : next j
150 for j = -siete to siete step x : process(j) : next j
160 for j = 555 to 550-y : process(j) : next j
170 for j = 22 to -28 step -tres : process(j) : next j
180 for j = 1927 to 1939 : process(j) : next j
190 for j = x to y step z : process(j) : next j
200 for j = (11^x) to (11^x)+uno : process(j) : next j
210 print " sum= ";sum
220 print "prod= ";prod
230 end
240 sub process(x)
250 sum = sum+abs(x)
260 if abs(prod) < (2^27) and x <> 0 then prod = prod*x
270 end sub</syntaxhighlight>
{{out}}
<pre> sum= 348173
prod= -793618560</pre>
 
=={{header|Common Lisp}}==
Line 1,212 ⟶ 1,319:
<pre>sum = 348173
prod = -793618560</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="text">prod = 1
sum = 0
x = 5
y = -5
z = -2
one = 1
three = 3
seven = 7
ranges[][] = [ [ -three (pow 3 3) three ] [ -seven seven x ] [ 555 (550 - y) ] [ 22 -28 (-three) ] [ 1927 1939 ] [ x y z ] [ (pow 11 x) (pow 11 x + one) ] ]
#
for i range len ranges[][]
j = ranges[i][0]
to = ranges[i][1]
inc = 1
if len ranges[i][] = 3
inc = ranges[i][2]
.
repeat
until inc > 0 and j > to or inc < 0 and j < to
sum += abs j
if abs prod < pow 2 27 and j <> 0
prod *= j
.
j += inc
.
.
print sum
print prod</syntaxhighlight>
 
=={{header|Eiffel}}==
Line 1,612 ⟶ 1,689:
 
=={{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 (mostly) 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 _28 BY -THREE
< 1927 TO 1939
< X TO Y BY Z
< (11^X) TO (11^X) + ONE
}} do.
SUM=: SUM+|J
if. ((|PROD)<2^27) * J~:0 do. PROD=: PROD*J end.
end.
echo ' SUM= ',":SUM
sum , prod
echo 'PROD= ',":PROD
)
}}0</syntaxhighlight>
 
Running this script produces this output:
<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)'
<pre> SUM= 348168
348173 _7.93619e8
PROD= _793618560</pre>
20j0 ": A
348173 _793618560
</pre>
 
=={{header|Java}}==
Line 1,797 ⟶ 1,881:
 
=={{header|Kotlin}}==
Nothing special here, justUsing a series of individual for loops.:
<syntaxhighlight lang="scalakotlin">// Version 1.2.70
 
import kotlin.math.abs
Line 1,833 ⟶ 1,917:
for (j in x downTo y step -z) process(j)
for (j in p..p + one) process(j)
System.out.printf("sum = % ,d\n", sum)
System.out.printf("prod = % ,d\n", prod)
}</syntaxhighlight>
 
{{output}}
<pre>
sum = 348,173
prod = -793,618,560
</pre>
 
The following version does it in a similar way to PL/I and Algol-60, i.e. without defining a function to process the loop, using a ''single'' loop, and without creating a list from which to iterate:
<syntaxhighlight lang="kotlin">
import kotlin.math.abs
import kotlin.math.pow
 
private infix fun Int.`^`(exponent: Int): Int = toDouble().pow(exponent).toInt()
 
fun main() {
var prod = 1
var sum = 0
val x = 5
val y = -5
val z = -2
val one = 1
val three = 3
val seven = 7
val p = 11 `^` x
 
for (j in sequenceOf(
-three..(3 `^` 3) step three,
-seven..seven step x,
555..550-y,
22 downTo -28 step three,
1927..1939,
x downTo y step -z,
p..p + one
).flatten()) {
sum += abs(j)
if (abs(prod) < (2 `^` 27) && j != 0) prod *= j
}
System.out.printf("sum = % ,d\n", sum)
System.out.printf("prod = % ,d\n", prod)
Line 2,135 ⟶ 2,259:
prod = -793,618,560
</pre>
 
=={{header|Phixmonti}}==
<syntaxhighlight lang="Phixmonti">/# Rosetta Code problem: https://rosettacode.org/wiki/Loops/With_multiple_ranges
by Galileo, 11/2022 #/
 
include ..\Utilitys.pmt
 
1 var prod
0 var total
+5 var x
-5 var y
-2 var z
1 var one
3 var three
7 var seven
 
( ( three neg 3 3 power three )
( seven neg seven x )
( 555 550 y - 1 )
( 22 -28 three neg )
( 1927 1939 1 )
( x y z )
( 11 x power 11 x power one + 1 ) )
 
len for
get for
dup abs total + var total
dup prod abs 2 27 power < and if prod * var prod else drop endif
endfor
endfor
 
( " sum = " total "\n" "prod = " prod ) lprint</syntaxhighlight>
{{out}}
<pre> sum = 348173
prod = -793618560
=== Press any key to exit ===</pre>
 
=={{header|Picat}}==
Line 2,192 ⟶ 2,352:
% ...
</syntaxhighlight>
 
 
 
=={{header|Prolog}}==
Line 2,561 ⟶ 2,719:
total = 348173
product = -793618560
</pre>
 
=={{header|RPL}}==
It's not exactly idiomatic, but it works.
≪ 5 -5 -2 1 3 7 → x y z one three seven
≪ { { '-three' '3^3' 'three' }
{ '-seven' 'seven' 'x' }
{ 555 '550-y' }
{ 22 -28 '-three' }
{ 1927 1939 }
{ 'x' 'y' 'z' }
{ '11^x' '11^x+one' } } → ranges
≪ 1 0
1 ranges SIZE '''FOR''' n
ranges n GET
'''IF''' DUP SIZE 2 == '''THEN''' 1 + '''END'''
LIST→ DROP EVAL → from to by
≪ from EVAL to EVAL '''FOR''' j
j ABS +
'''IF''' OVER ABS 2 27 ^ < j AND '''THEN''' SWAP j * SWAP '''END'''
by '''STEP'''
'''NEXT''' SWAP
≫ ≫ ≫ '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
2: 348173
1: -793618560
</pre>
 
Line 2,566 ⟶ 2,752:
Uses chaining of enumerables, which was introduced with Ruby 2.6
<syntaxhighlight lang="ruby">x, y, z, one, three, seven = 5, -5, -2, 1, 3, 7
 
 
enums = (-three).step(3**3, three) +
Line 2,719 ⟶ 2,906:
 
<syntaxhighlight lang="txrlisp">(defmacro mfor (:form f (var . range-triplets) . forms)
(with-gensyms (body toval stepval test)
^(let (,var)
(flet ((,body () ,*forms))
,*(append-each ((rt (tuples 3 range-triplets)))
(mac-param-bind f (initfrom testto step) rt
^((set ,var ,initfrom)
(for* () (,test)toval (,stepto) (,body)))))))))
(,stepval ,step)
(,test (if (<= ,var ,toval)
(fun <=) (fun >=))))
([,test ,var ,toval])
((inc ,var ,stepval))
(,body)))))))))
 
(let ((prod 1) (sum 0)
(x 5) (y -5) (z -2)
(one 1) (three 3) (seven 7))
(mfor (j (- three) (<= j (expt 3 3)) (inc j three)
(- seven) (<= j seven) (inc j x)
555 (<= j (- 550 y)) (inc j)1
22 (>= j -28) (dec j- three)
1927 (<= j 1939) (inc j 1)
x (>= j y) (inc j z)
(expt 11 x) (<= j (succ (expt 11 x))) (inc j)1)
(upd sum (+ (abs j)))
(if (and (< (abs prod) (ash 1 27))
Line 2,746 ⟶ 2,939:
 
<pre>sum = 348173; prod = -793618560</pre>
 
 
=={{header|uBasic/4tH}}==
Line 2,977 ⟶ 3,169:
=={{header|Wren}}==
{{trans|Go}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var prod = 1
Line 3,037 ⟶ 3,229:
}
 
SystemFmt.print("sum = %(Fmt.dc($,d", sum))")
SystemFmt.print("prod = %(Fmt.dc($,d", prod))")</syntaxhighlight>
 
{{out}}
9,476

edits