Loops/With multiple ranges: Difference between revisions

m
(Added Chipmunk Basic)
m (→‎{{header|Wren}}: Minor tidy)
 
(5 intermediate revisions by 3 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 1,843 ⟶ 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,879 ⟶ 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,641 ⟶ 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,646 ⟶ 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 3,062 ⟶ 3,169:
=={{header|Wren}}==
{{trans|Go}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var prod = 1
Line 3,122 ⟶ 3,229:
}
 
SystemFmt.print("sum = %(Fmt.dc($,d", sum))")
SystemFmt.print("prod = %(Fmt.dc($,d", prod))")</syntaxhighlight>
 
{{out}}
9,476

edits