Loops/Increment loop index within loop body: Difference between revisions
Content added Content deleted
(Added Wren) |
(→{{header|Wren}}: Now uses 'fmt' module. Changed preamble slightly.) |
||
Line 4,174: | Line 4,174: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|fmt}} |
|||
Although one can |
Although it might appear as though one can change the index variable within a ''for'' loop, it does in fact change a local copy of the variable and the iteration is not affected at all. Consequently, the only way to complete this task in Wren is to use a ''while'' loop. |
||
⚫ | |||
<lang ecmascript>import "/fmt" for Fmt |
|||
⚫ | |||
if (n < 2 || !n.isInteger) return false |
if (n < 2 || !n.isInteger) return false |
||
if (n%2 == 0) return n == 2 |
if (n%2 == 0) return n == 2 |
||
Line 4,187: | Line 4,190: | ||
} |
} |
||
return true |
return true |
||
} |
|||
var rset = Fn.new { |m, n| |
|||
var s = "%(n)" |
|||
var c = s.count |
|||
return (m > c) ? " " * (m - c) + s : s |
|||
} |
|||
var commatize = Fn.new { |n| |
|||
var s = "%(n)" |
|||
var le = s.count |
|||
var i = le - 3 |
|||
while (i >= 1) { |
|||
s = s[0...i] + "," + s[i..-1] |
|||
i = i - 3 |
|||
} |
|||
return s |
|||
} |
} |
||
Line 4,211: | Line 4,197: | ||
if (isPrime.call(i)) { |
if (isPrime.call(i)) { |
||
count = count + 1 |
count = count + 1 |
||
System.print("%( |
System.print("%(Fmt.d(2, count)): %(Fmt.dc(18, i))") |
||
i = 2 * i - 1 |
i = 2 * i - 1 |
||
} |
} |