Loops/Increment loop index within loop body: Difference between revisions
Content added Content deleted
m (→{{header|J}}) |
(add RPL) |
||
Line 4,358: | Line 4,358: | ||
n = 41 49,752,014,150,467 |
n = 41 49,752,014,150,467 |
||
n = 42 99,504,028,301,131</pre> |
n = 42 99,504,028,301,131</pre> |
||
=={{header|RPL}}== |
|||
<code>BPRIM?</code> is defined at [[Primality by trial division#RPL|Primality by trial division]] |
|||
RPL allows to modify both increment and index within a <code>FOR..NEXT</code> loop: |
|||
≪ 0 |
|||
42 1E15 '''FOR''' j |
|||
IF j R→B <span style="color:blue">BPRIM?</span> THEN |
|||
1 + DUP 1 DISP |
|||
j DUP 2 DISP |
|||
'''ELSE''' 1 '''END''' |
|||
'''IF''' OVER 42 == '''THEN''' 1E15 'j' STO '''END''' |
|||
'''STEP''' |
|||
≫ |
|||
It is nevertheless more idiomatic to use a <code>WHILE..REPEAT</code> loop: |
|||
≪ 0 42 |
|||
'''WHILE''' OVER 12 < '''REPEAT''' |
|||
'''IF''' DUP R→B <span style="color:blue">BPRIM?</span> THEN |
|||
SWAP 1 + DUP 1 DISP SWAP |
|||
DUP 2 DISP DUP |
|||
'''ELSE''' 1 '''END''' + |
|||
'''END''' DROP |
|||
≫ |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |