Loops/Increment loop index within loop body: Difference between revisions
Content added Content deleted
(Added Quackery.) |
|||
Line 4,149: | Line 4,149: | ||
41 -> 49,752,014,150,467 |
41 -> 49,752,014,150,467 |
||
42 -> 99,504,028,301,131</pre> |
42 -> 99,504,028,301,131</pre> |
||
=={{header|Quackery}}== |
|||
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 that exactly meets the specification of the task. |
|||
<syntaxhighlight lang="Quackery"> [ stack ] is f.action ( --> s ) |
|||
[ stack ] is f.end ( --> s ) |
|||
[ 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 ) |
|||
[ 1+ f.incr replace ] is incr ( n --> ) |
|||
[ true f.end replace ] is end ( --> ) |
|||
[ 1 false ]'[ |
|||
f.stacks witheach put |
|||
[ f.action share do |
|||
f.incr share |
|||
f.index tally |
|||
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 satisfied, 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 next incrementation to be that number ''plus 1''. This may seem counter-intuitive, but the task states "''in addition to the normal incrementation''" and, hey, a specification is a specification. |
|||
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. |
|||
Now we are ready for the task. |
|||
<code>prime</code>is defined at [[Miller–Rabin primality test#Quackery]]. |
|||
<syntaxhighlight lang="Quackery"> [ $ "" swap |
|||
number$ reverse |
|||
[ dup size 3 > while |
|||
3 split |
|||
dip join |
|||
dip [ char , join ] |
|||
again ] |
|||
join reverse echo$ ] is echo, ( n --> ) |
|||
0 |
|||
42 from |
|||
[ index prime if |
|||
[ 1+ |
|||
dup echo |
|||
say ": " |
|||
index echo, cr |
|||
index incr ] |
|||
dup 42 = if end ] |
|||
drop</syntaxhighlight> |
|||
{{out}} |
|||
<pre style="height:40ex">1: 43 |
|||
2: 89 |
|||
3: 179 |
|||
4: 359 |
|||
5: 719 |
|||
6: 1,439 |
|||
7: 2,879 |
|||
8: 5,779 |
|||
9: 11,579 |
|||
10: 23,159 |
|||
11: 46,327 |
|||
12: 92,657 |
|||
13: 185,323 |
|||
14: 370,661 |
|||
15: 741,337 |
|||
16: 1,482,707 |
|||
17: 2,965,421 |
|||
18: 5,930,887 |
|||
19: 11,861,791 |
|||
20: 23,723,597 |
|||
21: 47,447,201 |
|||
22: 94,894,427 |
|||
23: 189,788,857 |
|||
24: 379,577,741 |
|||
25: 759,155,483 |
|||
26: 1,518,310,967 |
|||
27: 3,036,621,941 |
|||
28: 6,073,243,889 |
|||
29: 12,146,487,779 |
|||
30: 24,292,975,649 |
|||
31: 48,585,951,311 |
|||
32: 97,171,902,629 |
|||
33: 194,343,805,267 |
|||
34: 388,687,610,539 |
|||
35: 777,375,221,081 |
|||
36: 1,554,750,442,183 |
|||
37: 3,109,500,884,389 |
|||
38: 6,219,001,768,781 |
|||
39: 12,438,003,537,571 |
|||
40: 24,876,007,075,181 |
|||
41: 49,752,014,150,467 |
|||
42: 99,504,028,301,131</pre> |
|||
=={{header|R}}== |
=={{header|R}}== |
||
R cannot complete this task with a for loop. See https://stackoverflow.com/a/5913329/ . Instead, we must go down the same path as the Kotlin solution. Because it is sufficient for numbers this small, we will save ourselves some work and use the gmp library's isprime function for checking if a number is prime. |
R cannot complete this task with a for loop. See https://stackoverflow.com/a/5913329/ . Instead, we must go down the same path as the Kotlin solution. Because it is sufficient for numbers this small, we will save ourselves some work and use the gmp library's isprime function for checking if a number is prime. |