Loops/Increment loop index within loop body: Difference between revisions
Loops/Increment loop index within loop body (view source)
Revision as of 16:33, 3 May 2024
, 1 month ago→{{header|EasyLang}}
(Added Quackery.) |
|||
(9 intermediate revisions by 4 users not shown) | |||
Line 1,907:
=={{header|EasyLang}}==
{{trans|FreeBASIC}}
<syntaxhighlight
fastfunc isprim num .
while i <= sqrt num
if num mod i = 0
return 0
.
i +=
.
return 1
Line 1,944 ⟶ 1,941:
n=42 99504028301131
</pre>
=={{header|EMal}}==
{{trans|Java}}
<syntaxhighlight lang="emal">
int LIMIT = 42
fun isPrime = logic by int n
if n % 2 == 0 do return n == 2 end
if n % 3 == 0 do return n == 3 end
int d = 5
while d * d <= n
if n % d == 0 do return false end
d += 2
if n % d == 0 do return false end
d += 4
end
return true
end
for int i = LIMIT, int n = 0; n < LIMIT; ++i
if not isPrime(i) do continue end
++n
writeLine("n = " + n + ",\ti = " + i)
i += i - 1
end
</syntaxhighlight>
=={{header|F_Sharp|F#}}==
Line 4,152 ⟶ 4,173:
=={{header|Quackery}}==
===With indexed loop word===
Quackery has an iterative looping word that can change its step size mid-iteration (see [[Loops/For with a specified step#Quackery]]) but it is not well suited to this task. A better solution (i.e. more idiomatic) is to define a new looping word, <code>from</code>, that exactly meets the specification of the task.
<syntaxhighlight lang="Quackery"> [ stack ] is f.
[ stack ] is f.
[ stack ] is f.incr ( --> s )
[ stack ] is f.index ( --> s )
[ ' [ f.action f.end
f.incr f.index ] ] is f.stacks ( --> [ )
[ f.index share ] is index ( --> n )
[
[ true f.end replace ] is end (
[ 1 false ]'[
Line 4,175 ⟶ 4,198:
1 incr
f.end share until ]
f.stacks witheach release ] is from ( n --> )</syntaxhighlight>
<code>from</code> takes its initial index from the stack, and performs the nest that follows it repeatedly until the ending condition is set to <code>true</code>, incrementing the index at the end of each iteration.
<code>index</code> returns the current index.
The default increment is <code>1</code>, but this can be overridden for a single iteration by the word <code>incr</code> within the nest being performed by <code>from</code>. <code>incr</code> takes a number from the stack, and sets the increment to that number for the current iteration. If <code>incr</code> is performed within some iterations but not all, the increment will be <code>1</code> for those iterations where it is not performed.
The task states "''in addition to the normal incrementation''" but this is counter-intuitive. To make the loop task compliant you will need to precede <code>f.incr</code> with <code>1+</code> in <code>incr</code>. You will also need to delete the <code>1+</code> before <code>incr</code> in the task code below.
The word <code>end</code> sets the ending condition to <code>true</code>, so the loop will end at the end of the current iteration.
As with other iterative looping words in Quackery (e.g. <code>times</code>, <code>witheach</code>, etc.) the word <code>done</code> will terminate the current iteration immediately.
Line 4,201 ⟶ 4,223:
dip [ char , join ]
again ]
join reverse echo$ ]
0
Line 4,210 ⟶ 4,232:
say ": "
index echo, cr
index 1+ incr ]
dup 42 = if end ]
drop</syntaxhighlight>
Line 4,258 ⟶ 4,280:
41: 49,752,014,150,467
42: 99,504,028,301,131</pre>
===Without indexed loop word===
<code>prime</code>is defined at [[Miller–Rabin primality test#Quackery]].
<code>echo,</code> is as above.
<syntaxhighlight lang="Quackery"> 0 42
[ dup prime if
[ dip 1+
over echo
say ": "
dup echo, cr
dup + ]
1+
over 42 = until ]
2drop
</syntaxhighlight>
{{out}}
Output is as above.
=={{header|R}}==
Line 5,174 ⟶ 5,218:
{{libheader|Wren-fmt}}
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.
<syntaxhighlight lang="
var isPrime = Fn.new { |n|
Line 5,270 ⟶ 5,314:
return True
end sub</syntaxhighlight>
=={{header|zig}}==
{{trans|c}}
<syntaxhighlight lang="zig">const std = @import("std");
pub fn isPrime(n: i64) bool {
if (@mod(n, 2) == 0) return n == 2;
if (@mod(n, 3) == 0) return n == 3;
var d: i64 = 5;
while (d * d <= n) {
if (@mod(n, d) == 0) return false;
d += 2;
if (@mod(n, d) == 0) return false;
d += 4;
}
return true;
}
pub fn main() !void {
var i: i64 = 42;
var n: i64 = 0;
while (n < 42) : (i += 1) {
if (isPrime(i)) {
n += 1;
std.debug.print("n = {d}\t{d}\n", .{ n, i });
i += i - 1;
}
}
}</syntaxhighlight>
{{out}}
<pre style="height:35ex">
n = 1 43
n = 2 89
n = 3 179
n = 4 359
n = 5 719
n = 6 1439
n = 7 2879
n = 8 5779
n = 9 11579
n = 10 23159
n = 11 46327
n = 12 92657
n = 13 185323
n = 14 370661
n = 15 741337
n = 16 1482707
n = 17 2965421
n = 18 5930887
n = 19 11861791
n = 20 23723597
n = 21 47447201
n = 22 94894427
n = 23 189788857
n = 24 379577741
n = 25 759155483
n = 26 1518310967
n = 27 3036621941
n = 28 6073243889
n = 29 12146487779
n = 30 24292975649
n = 31 48585951311
n = 32 97171902629
n = 33 194343805267
n = 34 388687610539
n = 35 777375221081
n = 36 1554750442183
n = 37 3109500884389
n = 38 6219001768781
n = 39 12438003537571
n = 40 24876007075181
n = 41 49752014150467
n = 42 99504028301131
</pre>
=={{header|zkl}}==
|