Loops/Increment loop index within loop body: Difference between revisions

m
No edit summary
 
(29 intermediate revisions by 16 users not shown)
Line 58:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F is_prime(n)
L(x) (2, 3)
I n % x == 0
Line 77:
print(‘n = #2 #16’.format(n, i))
i += i - 1
i++</langsyntaxhighlight>
 
{{out}}
Line 101:
This task is a good example of the use of ED instruction to format a number.
For macro use (IF,DO,...), see [[360_Assembly_macros#360_Assembly_Structured_Macros|Structured Macros]].
<langsyntaxhighlight lang="360asm">* Loops/Increment loop index within loop body - 16/07/2018
LOOPILWB PROLOG
SR R6,R6 i=0
Line 173:
ZN DS CL20
REGEQU
END LOOPILWB</langsyntaxhighlight>
{{out}}
<pre style="height:40ex">
Line 221:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program loopinc64.s */
Line 471:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 520:
=={{header|Ada}}==
Ada does not allow the loop counter of a FOR loop to be modified in the loop. This example uses a simple Ada loop. The example also uses an Ada fixed decimal type to provide the comma insertion for the output.
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Text_IO.Editing; use Ada.Text_IO.Editing;
Line 571:
end loop;
end Main;
</syntaxhighlight>
</lang>
{{output}}
<pre>
Line 620:
=={{header|ALGOL 68}}==
In Algol 68, the FOR loop counter cannot be modified in the loop. This uses a WHILE loop testing at the top but is otherwise largely a translation of the Kotlin entry.
<langsyntaxhighlight lang="algol68">BEGIN
# returns TRUE if n is prime, FALSE otherwise #
PROC is prime = ( LONG INT n )BOOL:
Line 654:
i +:= 1
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 699:
n = 41 49752014150467
n = 42 99504028301131
</pre>
 
=={{header|Amazing Hopper}}==
<syntaxhighlight lang="c">
#include <jambo.h>
Main
Set break
Set decimal '0'
i=42, c=0
Loop if ( #(c<42) )
Set 'i', When ( Is prime? ){
++c, Printnl( "n = ", c,"\t",Just right(20,Money(0,i) ) )
#( i += (i - 1) )
}
++i
Back
End
</syntaxhighlight>
{{out}}
<pre>
n = 1 43
n = 2 89
n = 3 179
n = 4 359
n = 5 719
n = 6 1,439
n = 7 2,879
n = 8 5,779
n = 9 11,579
n = 10 23,159
n = 11 46,327
n = 12 92,657
n = 13 185,323
n = 14 370,661
n = 15 741,337
n = 16 1,482,707
n = 17 2,965,421
n = 18 5,930,887
n = 19 11,861,791
n = 20 23,723,597
n = 21 47,447,201
n = 22 94,894,427
n = 23 189,788,857
n = 24 379,577,741
n = 25 759,155,483
n = 26 1,518,310,967
n = 27 3,036,621,941
n = 28 6,073,243,889
n = 29 12,146,487,779
n = 30 24,292,975,649
n = 31 48,585,951,311
n = 32 97,171,902,629
n = 33 194,343,805,267
n = 34 388,687,610,539
n = 35 777,375,221,081
n = 36 1,554,750,442,183
n = 37 3,109,500,884,389
n = 38 6,219,001,768,781
n = 39 12,438,003,537,571
n = 40 24,876,007,075,181
n = 41 49,752,014,150,467
n = 42 99,504,028,301,131
</pre>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program loopinc96.s */
Line 1,200 ⟶ 1,262:
pop {r4,r5,lr} @ restaur registers
bx lr @ return
</syntaxhighlight>
</lang>
{{out}}
<Pre>
Line 1,250 ⟶ 1,312:
=={{header|Arturo}}==
{{trans|Python}}
<langsyntaxhighlight lang="rebol">i: 42
n: 0
 
Line 1,262 ⟶ 1,324:
i: i + 1
]
]</langsyntaxhighlight>
{{out}}
<pre>n = 1 43
Line 1,308 ⟶ 1,370:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">i:= 42, n:= 1, result := ""
while (n<43){
if isPrime(i)
Line 1,328 ⟶ 1,390:
}
return true
}</langsyntaxhighlight>
{{out}}
<pre>1: 43
Line 1,374 ⟶ 1,436:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f LOOPS_INCREMENT_LOOP_INDEX_WITHIN_LOOP_BODY.AWK
BEGIN {
Line 1,399 ⟶ 1,461:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,448 ⟶ 1,510:
 
=={{header|BASIC256}}==
<langsyntaxhighlight BASIC256lang="basic256">function isPrime(number)
if (number % 2 = 0) or (number % 3 = 0) then return false
lim = sqr(number)
Line 1,469 ⟶ 1,531:
i += 1
end while
end</langsyntaxhighlight>
 
 
Line 1,477 ⟶ 1,539:
 
The 'thousands separator' aspect (using the ' flag in printf and setting the locale appropriately) works fine when compiled with gcc on Ubuntu 14.04 but may not work on some other systems as this is not a standard flag.
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <locale.h>
 
Line 1,506 ⟶ 1,568:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 1,514 ⟶ 1,576:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">
using System;
using System.Globalization;
Line 1,549 ⟶ 1,611:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,597 ⟶ 1,659:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">
#include "stdafx.h"
#include <iostream>
Line 1,635 ⟶ 1,697:
}
return 0;
}</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">
(defun primep (n) ; https://stackoverflow.com/questions/15817350/
(cond ((= 2 n) t) ; Hard-code "2 is a prime"
Line 1,656 ⟶ 1,718:
(format t "~&~5<~d~;->~>~20<~:d~>" c i) ; Display count of primes found and the prime
(incf i (decf i))))) ; Increment index to previous index plus the prime
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,705 ⟶ 1,767:
{{libheader| System.SysUtils}}
{{Trans|C#}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Increment_loop_index_within_loop_body;
 
Line 1,760 ⟶ 1,822:
end;
readln;
end.</langsyntaxhighlight>
{{out}}
Same of [[#C#]].
Line 1,766 ⟶ 1,828:
=={{header|Dyalect}}==
 
<langsyntaxhighlight Dyalectlang="dyalect">func isPrime(number) {
if number <= 1 {
return false
Line 1,796 ⟶ 1,858:
}
i += 1
}</langsyntaxhighlight>
 
Output:
Line 1,842 ⟶ 1,904:
n = 41 49752014150467
n = 42 99504028301131</pre>
 
=={{header|EasyLang}}==
{{trans|FreeBASIC}}
<syntaxhighlight>
fastfunc isprim num .
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
counter = 0
maxnum = pow 2 53
for i = 42 to maxnum
if isprim i = 1
counter += 1
print "n=" & counter & " " & i
if counter >= 42
break 1
.
i += i - 1
.
.
</syntaxhighlight>
{{out}}
<pre>
n=1 43
n=2 89
n=3 179
.
.
n=41 49752014150467
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#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// Well I don't do loops. Nigel Galloway: March 17th., 2019. Let me try to explain where the loopy variables are, for the imperatively constrained.
// cUL allows me to claim the rather trivial extra credit (commas in the numbers)
Line 1,855 ⟶ 1,978:
// unfold is sort of a loop with fG as an internal loop incremented by the exit value of the internal loop in this case.
Seq.unfold(fun n->let n=fG n in Some(n,n+n)) 42UL|>Seq.take 42|>Seq.iteri(fun n g->printfn "%2d -> %s" (n+1) (cUL g))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,905 ⟶ 2,028:
Explicit loop indices are non-idiomatic, but Factor is certainly capable of using them. Factor has a for loop near-equivalent, <code><range> [ ] each</code>, but since it doesn't mesh well with mutation, a while loop is used.
=== Using two numbers on the data stack ===
<langsyntaxhighlight lang="factor">USING: formatting kernel math math.primes
tools.memory.private ;
IN: rosetta-code.loops-inc-body
Line 1,919 ⟶ 2,042:
[ 1 + ] dip
] while
2drop</langsyntaxhighlight>
=== Using lexical variables ===
Factor provides lexical variables for situations where they improve readability.
<langsyntaxhighlight lang="factor">USING: formatting kernel math math.primes
tools.memory.private ;
IN: rosetta-code.loops-inc-body
Line 1,937 ⟶ 2,060:
i 1 + i!
] while
]</langsyntaxhighlight>
{{out}}
<pre>
Line 1,986 ⟶ 2,109:
=={{header|Fortran}}==
Fortran does not allow to modify the index inside the loop.
<langsyntaxhighlight lang="fortran">do i=1,10
write(*,*) i
i=i+1
end do</langsyntaxhighlight>
<pre>
Error - I is currently being used as a DO or implied DO control variable
Line 1,996 ⟶ 2,119:
 
===Fortran 95===
<langsyntaxhighlight lang="fortran">! Loops Increment loop index within loop body - 17/07/2018
integer*8 n
imax=42
Line 2,030 ⟶ 2,153:
return
EndIf
EndFunction</langsyntaxhighlight>
{{out}}
<pre style="height:60ex">
Line 2,079 ⟶ 2,202:
===Fortran IV===
The limit is set to 25 due to the size of integer in Fortran IV.
<langsyntaxhighlight lang="fortran">C LOOPS INCREMENT LOOP INDEX WITHIN LOOP BODY - 17/07/2018
IMAX=25
I=0
Line 2,110 ⟶ 2,233:
50 ISPRIME=1
RETURN
END</langsyntaxhighlight>
{{out}}
<pre style="height:60ex">
Line 2,141 ⟶ 2,264:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 18-01-2019
' compile with: fbc -s console
 
Line 2,178 ⟶ 2,301:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre style="height:40ex">n = 1 43
Line 2,227 ⟶ 2,350:
 
The 'thousands separator' aspect is dealt with by a couple of external packages (in the 'import' declarations) which can be installed using 'go get'.
<langsyntaxhighlight lang="go">package main
 
import(
Line 2,266 ⟶ 2,389:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,272 ⟶ 2,395:
Same as Kotlin entry
</pre>
 
=={{header|Haskell}}==
No index mutations or loops. Recursion is used.
<langsyntaxhighlight lang="haskell">import Data.List
import Control.Monad (guard)
 
Line 2,300 ⟶ 2,424:
display (i, p) = show i ++ " " ++ digitGroup p
 
main = mapM_ (putStrLn . display) $ take 42 $ showPrime 42 1</langsyntaxhighlight>
{{out}}
<pre>
Line 2,349 ⟶ 2,473:
And for minor variation, we could import isPrime from Data.Numbers.Primes, and define the comma-grouping of large integers in terms of chunksof:
 
<langsyntaxhighlight lang="haskell">import Data.Numbers.Primes
import Data.List (intercalate)
import Data.List.Split (chunksOf)
Line 2,367 ⟶ 2,491:
 
main :: IO ()
main = mapM_ (putStrLn . showPair) (take 42 $ series 42 1)</langsyntaxhighlight>
 
=={{header|Haxe}}==
Haxe's for-loop does allow the index to be modified in the body of the loop, so a while-loop is used instead.
<langsyntaxhighlight lang="haxe">using StringTools;
import haxe.Int64;
 
Line 2,407 ⟶ 2,531:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,456 ⟶ 2,580:
 
=={{header|J}}==
An idiomatic approach:
Fun with j. The verb tacit_loop implements the computation.
<syntaxhighlight lang=J> (,.~#\)}:(}:, (,1&p: # _1 2&p.)@:>:@{:)^:(42 >: #)^:_ x: 42
<lang j>
1 43
tacit_loop =: _1&(>:@:{`[`]})@:(, (1&p: # _1 2&p.)@:{:)@:]^:(0 ~: (>: #))^:_ x:
2 89
</lang>
3 179
Now derive it from the python solution. The monadic verb loop fairly straightforwardly matches the python solution except that loop returns the vector of computed values rather than displays them.
4 359
<lang j>
5 719
6 1439
7 2879
8 5779
9 11579
10 23159
11 46327
12 92657
13 185323
14 370661
15 741337
16 1482707
17 2965421
18 5930887
19 11861791
20 23723597
21 47447201
22 94894427
23 189788857
24 379577741
25 759155483
26 1518310967
27 3036621941
28 6073243889
29 12146487779
30 24292975649
31 48585951311
32 97171902629
33 194343805267
34 388687610539
35 777375221081
36 1554750442183
37 3109500884389
38 6219001768781
39 12438003537571
40 24876007075181
41 49752014150467
42 99504028301131
</syntaxhighlight>
 
Most of the remainder of this treatment ignores output formatting issues and focuses purely on algorithmic issues.
 
The loop index from J's <code>for.</code> is read only. But we can use a while loop to achieve the effect of a mutable loop index.
 
Here are some sketches, starting with that concept and developing some plausible alternative approaches:
 
A variant derived from the python solution (except this loop returns the list of computed values rather than displays them):
<syntaxhighlight lang="j">
isPrime =: 1&p:
assert 1 1 0 -: isPrime 2 3 4 NB. test and example
Line 2,477 ⟶ 2,649:
n
)
</syntaxhighlight>
</lang>
Store the vector of indexes using its tail as the current index, removing the `n' variable. In doing so the last item of `i' is not part of the solution, hence change less than to less or equal, and discard the tail value. Also extract the conversion to extended precision x: .
<syntaxhighlight lang="j">
<lang J>
loop =: verb define@:x:
i =. y
Line 2,490 ⟶ 2,662:
}: i
)
</syntaxhighlight>
</lang>
 
Replace the "if" statement with a computation. This one works by appending onto the solution vector isPrime copies of the proposed new index.
<syntaxhighlight lang="j">
<lang J>
loop =: verb define@:x:
i =. y
Line 2,502 ⟶ 2,674:
}: i
)
</syntaxhighlight>
</lang>
Names are an issue brought forth in the j forums. Names have most meaning to the person who wrote them, so there's a bit of J philosophy that says "show the code". J doesn't enforce "code only", and definitions can encapsulate useful chunks of code. If the names I've chosen don't work in your experience or language you could replace them with `a' and `b'.
<syntaxhighlight lang="j">
<lang J>
save_if_prime =: , (isPrime # _1 2&p.)@:{:
increment_tail =: _1&(>:@:{`[`]})
Line 2,516 ⟶ 2,688:
}: i
)
</syntaxhighlight>
</lang>
Why make two assignments when j can increment at save?
<syntaxhighlight lang="j">
<lang J>
loop =: verb define@:x:
i =. y
Line 2,526 ⟶ 2,698:
}: i
)
</syntaxhighlight>
</lang>
Next replace the while loop with double application of J's generalized power conjunction.
<syntaxhighlight lang="j">
<lang J>
While =: conjunction def 'u^:(0~:v)^:_'
 
Line 2,535 ⟶ 2,707:
}: increment_tail@:save_if_prime While(y >: #) i
)
</syntaxhighlight>
</lang>
By inspection the variable `i' doesn't contribute anything useful whatsoever. The verb's argument, y, remains.
Finally, implemented as an hook [http://www.jsoftware.com/help/dictionary/dictf.htm verb trains] with 'y' and `i' as left ([) and right (]) arguments the complete definitions for tacit_loop are
<syntaxhighlight lang="j">
<lang J>
isPrime =: 1&p:
save_if_prime =: , (isPrime # _1 2&p.)@:{:
Line 2,544 ⟶ 2,716:
While =: conjunction def 'u^:(0~:v)^:_'
tacit_loop =: [: }: (increment_tail@:save_if_prime@:]While(>: #) x:)
</syntaxhighlight>
</lang>
Include the index numbers with demonstration:
<syntaxhighlight lang="j">
<lang J>
9!:37 ] 0 2048 0 222 NB. output control permit lines of 2^11 columns
 
Line 2,558 ⟶ 2,730:
[: }: (_1&(>:@:{`[`]})@:(, (1&p: # _1 2&p.)@:{:)@:]^:(0 ~: (>: #))^:_ x:)
</syntaxhighlight>
</lang>
If the loop must require the output side effect, this save_if_prime definition does the trick. Without the output hook it is probably more efficient than the copying version because it evaluates the hook <pre>(, _1 2&p.@:{:)</pre> only when isPrime is true.
<syntaxhighlight lang="j">
<lang J>
extra_credit =: ([: }. ,@(',' ,.~ _3 [\ ])&.|.@:":)&>
show =: [ ([: echo@:deb@:({. , ' ' , {:)@:extra_credit # , {:)
Line 2,607 ⟶ 2,779:
41 49,752,014,150,467
42 99,504,028,301,131
</syntaxhighlight>
</lang>
 
=={{header|Java}}==
The following uses a 'for' rather than a 'do/while' loop but otherwise is similar to the Kotlin
entry.
<langsyntaxhighlight lang="java">public class LoopIncrementWithinBody {
 
static final int LIMIT = 42;
Line 2,639 ⟶ 2,811:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,645 ⟶ 2,817:
Same as Kotlin entry
</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
jq's looping constructs that have a loop index do not allow that index to be modified within the loop
as such indices are "read-only", but all the looping constructs allow an index to be
be defined and modified, as illustrated by the following example, which uses "i" as the loop index.
 
This entry uses the jq implementation of is_prime as shown at
[[Erd%C5%91s-primes#jq]].
<syntaxhighlight lang="jq">{i:42, count:0}
| while( .count <= 42;
.emit = null
| .i += 1
| if .i|is_prime
then
.count += 1
| .emit = "count at \(.i) is \(.count)"
| .i = .i + .i - 1
else .
end )
| select(.emit).emit</syntaxhighlight>
{{out}}
<pre>
count at 43 is 1
count at 89 is 2
count at 179 is 3
count at 359 is 4
count at 719 is 5
count at 1439 is 6
count at 2879 is 7
count at 5779 is 8
count at 11579 is 9
count at 23159 is 10
count at 46327 is 11
count at 92657 is 12
count at 185323 is 13
count at 370661 is 14
count at 741337 is 15
count at 1482707 is 16
count at 2965421 is 17
count at 5930887 is 18
count at 11861791 is 19
count at 23723597 is 20
count at 47447201 is 21
count at 94894427 is 22
count at 189788857 is 23
count at 379577741 is 24
count at 759155483 is 25
count at 1518310967 is 26
count at 3036621941 is 27
count at 6073243889 is 28
count at 12146487779 is 29
count at 24292975649 is 30
count at 48585951311 is 31
count at 97171902629 is 32
count at 194343805267 is 33
count at 388687610539 is 34
count at 777375221081 is 35
count at 1554750442183 is 36
count at 3109500884389 is 37
count at 6219001768781 is 38
count at 12438003537571 is 39
count at 24876007075181 is 40
count at 49752014150467 is 41
count at 99504028301131 is 42
</pre>
 
 
=={{header|Julia}}==
Julia's <code>for</code> loop iterator is an iterator type which cannot be incremented as a simple variable would to change looping.
<langsyntaxhighlight lang="julia">using Primes, Formatting
 
function doublemyindex(n=42)
Line 2,665 ⟶ 2,906:
 
doublemyindex()
</langsyntaxhighlight> {{output}} <pre>
The index is 1 and 43 is prime.
The index is 2 and 89 is prime.
Line 2,714 ⟶ 2,955:
 
So instead we use a do/while loop here which has no such restrictions.
<langsyntaxhighlight lang="scala">// version 1.2.60
 
fun isPrime(n: Long): Boolean {
Line 2,741 ⟶ 2,982:
}
while (n < 42)
}</langsyntaxhighlight>
{{out}}
<pre style="height:35ex">n = 1 43
Line 2,792 ⟶ 3,033:
The following version uses a tail recursive function rather than a while loop to achieve the same effect:
 
<langsyntaxhighlight lang="scala">// version 1.2.60
 
fun isPrime(n: Long): Boolean {
Line 2,821 ⟶ 3,062:
fun main(args: Array<String>) {
loop(42, 0)
}</langsyntaxhighlight>
 
{{out}}
Line 2,827 ⟶ 3,068:
Same as 'while' loop version.
</pre>
 
=={{header|Ksh}}==
<syntaxhighlight lang="ksh">
#!/bin/ksh
 
# Increment loop index within loop body
 
# # Variables:
#
integer INDX_START=42 N_PRIMES=42
 
# # Functions:
#
 
# # Function _isprime(n) return 1 for prime, 0 for not prime
#
function _isprime {
typeset _n ; integer _n=$1
typeset _i ; integer _i
 
(( _n < 2 )) && return 0
for (( _i=2 ; _i*_i<=_n ; _i++ )); do
(( ! ( _n % _i ) )) && return 0
done
return 1
}
 
######
# main #
######
integer i n=0
for ((i=INDX_START; n<N_PRIMES; i++)); do
_isprime ${i}
if (( $? )); then
printf "%,18d is prime, %2d primes found(so far)\n" ${i} $((++n))
(( i+=$i ))
fi
done</syntaxhighlight>
{{out}}<pre>
43 is prime, 1 primes found(so far)
89 is prime, 2 primes found(so far)
179 is prime, 3 primes found(so far)
359 is prime, 4 primes found(so far)
719 is prime, 5 primes found(so far)
1,439 is prime, 6 primes found(so far)
2,879 is prime, 7 primes found(so far)
5,779 is prime, 8 primes found(so far)
11,579 is prime, 9 primes found(so far)
23,159 is prime, 10 primes found(so far)
46,327 is prime, 11 primes found(so far)
92,657 is prime, 12 primes found(so far)
185,323 is prime, 13 primes found(so far)
370,661 is prime, 14 primes found(so far)
741,337 is prime, 15 primes found(so far)
1,482,707 is prime, 16 primes found(so far)
2,965,421 is prime, 17 primes found(so far)
5,930,887 is prime, 18 primes found(so far)
11,861,791 is prime, 19 primes found(so far)
23,723,597 is prime, 20 primes found(so far)
47,447,201 is prime, 21 primes found(so far)
94,894,427 is prime, 22 primes found(so far)
189,788,857 is prime, 23 primes found(so far)
379,577,741 is prime, 24 primes found(so far)
759,155,483 is prime, 25 primes found(so far)
1,518,310,967 is prime, 26 primes found(so far)
3,036,621,941 is prime, 27 primes found(so far)
6,073,243,889 is prime, 28 primes found(so far)
12,146,487,779 is prime, 29 primes found(so far)
24,292,975,649 is prime, 30 primes found(so far)
48,585,951,311 is prime, 31 primes found(so far)
97,171,902,629 is prime, 32 primes found(so far)
194,343,805,267 is prime, 33 primes found(so far)
388,687,610,539 is prime, 34 primes found(so far)
777,375,221,081 is prime, 35 primes found(so far)
1,554,750,442,183 is prime, 36 primes found(so far)
3,109,500,884,389 is prime, 37 primes found(so far)
6,219,001,768,781 is prime, 38 primes found(so far)
12,438,003,537,571 is prime, 39 primes found(so far)
24,876,007,075,181 is prime, 40 primes found(so far)
49,752,014,150,467 is prime, 41 primes found(so far)
99,504,028,301,131 is prime, 42 primes found(so far)</pre>
 
=={{header|Lambdatalk}}==
We use the javascript bigInts and the isPrime primitive working on big integers.
 
<syntaxhighlight lang="scheme">
 
{isPrime 11}
-> true
 
{isPrime 99504028301131}
-> true
 
{def upto
{def upto.loop
{lambda {:max :i :n}
{if {> :n :max}
then
else {if {isPrime :i}
then {tr {td n = :n} {td {@ style="text-align:right"} :i}}
{upto.loop :max
{BI.+ :i {BI.- :i 1}}
{BI.+ :n 1}}
else {upto.loop :max
{BI.+ :i 1}
:n} }}}}
{lambda {:n}
{upto.loop :n 42 1} }}
-> upto
 
{table
{upto 42}
}
->
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
</syntaxhighlight>
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">-- Returns boolean indicate whether x is prime
function isPrime (x)
if x < 2 then return false end
Line 2,849 ⟶ 3,247:
end
i = i + 1
end</langsyntaxhighlight>
{{out}}
<pre>n = 1 43
Line 2,895 ⟶ 3,293:
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module CheckIt {
Function IsPrime (x) {
Line 2,926 ⟶ 3,324:
}
CheckIt
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,935 ⟶ 3,333:
=={{header|Maple}}==
A translation of Kotlin entry
<langsyntaxhighlight Maplelang="maple">i := 42:
count := 0:
while(count < 42) do
Line 2,944 ⟶ 3,342:
i := 2*i -1:
end if:
end do:</langsyntaxhighlight>
{{Out|Output}}
<pre>n=1 43
Line 2,990 ⟶ 3,388:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">{i, n} = {42, 0};
While[n < 42,
If[PrimeQ[i],
Line 2,997 ⟶ 3,395:
];
i++;
]</langsyntaxhighlight>
{{out}}
<pre>n=0 43
Line 3,044 ⟶ 3,442:
=={{header|Microsoft Small Basic}}==
Small Basic allows to modify the index inside the loop.
<langsyntaxhighlight lang="smallbasic">'Loops Increment loop index within loop body - 16/07/2018
imax=42
i=0
Line 3,100 ⟶ 3,498:
nn=Text.GetSubText(space,1,Text.GetLength(space)-Text.GetLength(nn))+nn
ret_format_n=nn
EndSub 'format_n</langsyntaxhighlight>
{{out}}
<pre style="height:60ex">
Line 3,149 ⟶ 3,547:
=={{header|Nanoquery}}==
{{trans|Java}}
<langsyntaxhighlight Nanoquerylang="nanoquery">limit = 42
 
def isPrime(n)
Line 3,176 ⟶ 3,574:
i += i - 1
end
end</langsyntaxhighlight>
 
{{out}}
Line 3,223 ⟶ 3,621:
 
=={{header|NewLISP}}==
<langsyntaxhighlight lang="newlisp">
#! /usr/local/bin/newlisp
 
Line 3,261 ⟶ 3,659:
 
(exit)
</syntaxhighlight>
</lang>
 
<pre>
Line 3,311 ⟶ 3,709:
=={{header|Nim}}==
In Nim, loop index is read-only, so we have to use a normal variable and increment it as needed.
<langsyntaxhighlight lang="nim">
import strformat
from strutils import insertSep
Line 3,340 ⟶ 3,738:
 
main()
</syntaxhighlight>
</lang>
{{out}}
<pre style="height:50ex">
Line 3,391 ⟶ 3,789:
{{trans|Kotlin}}
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use ntheory qw(is_prime);
 
$i = 42;
Line 3,407 ⟶ 3,805:
$s =~ s/,$//;
$s = reverse $s;
}</langsyntaxhighlight>
{{out}}
<pre style="height:30ex"> 1 43
Line 3,454 ⟶ 3,852:
=={{header|Phix}}==
Phix does not allow for loop variables to be modified, so we must use a while loop and manual increment for this sort of thing.
<!--<langsyntaxhighlight Phixlang="phix">-->
<span style="color: #004080;">atom</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">42</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">n</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">42</span> <span style="color: #008080;">do</span>
Line 3,464 ⟶ 3,862:
<span style="color: #000000;">i</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 3,513 ⟶ 3,911:
=={{header|Python}}==
===Procedural===
<langsyntaxhighlight Pythonlang="python">def isPrime(n):
for x in 2, 3:
if not n % x:
Line 3,532 ⟶ 3,930:
print('n = {:2} {:20,}'.format(n, i))
i += i - 1
i += 1</langsyntaxhighlight>
{{out}}
<pre>n = 1 43
Line 3,581 ⟶ 3,979:
This task is defined in terms of procedural 'loops', which can generally be understood in functional terms as implementations of folds or maps. An analogous functional construction here might be a non-finite generator, or the iterative application of a function (over a tuple of values) to a seed value, for example:
 
<langsyntaxhighlight lang="python">'''Loops/Increment loop index within loop body.'''
 
from itertools import islice, takewhile
Line 3,728 ⟶ 4,126:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre> 1 -> 43
Line 3,772 ⟶ 4,170:
41 -> 49,752,014,150,467
42 -> 99,504,028,301,131</pre>
 
=={{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.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 )
 
[ f.incr replace ] is incr ( n --> )
 
[ true f.end replace ] is end ( b --> )
 
[ 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 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.
 
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 1+ 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>
 
===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}}==
R cannot complete this task with a for loop. See https://stackoverflow.com/a/5913329/10319707 . 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.
<langsyntaxhighlight Rlang="rsplus">i <- 42
primeCount <- 0
while(primeCount < 42)
{
if(gmp::isprime(i) == 2)#1 means "probably prime" and won't come up for numbers this small, 2 is what we want.
{
primeCount <- primeCount + 1
extraCredit <- format(i, big.mark=",", scientific = FALSE)
cat("Prime count:", paste0(primeCount, ";"), "The prime just found was:", extraCredit, "\n")
i <- i + i#This is missing the -1 from the Kotlin solution. There is no need to check i + i (it's even).
}
i <- i + 1
}</langsyntaxhighlight>
{{Out}}
<pre>Prime count: 1; The prime just found was: 43
Line 3,830 ⟶ 4,361:
Prime count: 41; The prime just found was: 49,752,014,150,467
Prime count: 42; The prime just found was: 99,504,028,301,131</pre>
 
=={{header|Racket}}==
 
Racket's <code>for</code> doesn't allow modification of index on the fly. The usual idiom for writing this kind of loop is to use named let, as shown here.
 
<langsyntaxhighlight lang="racket">#lang racket
 
(require math/number-theory)
Line 3,852 ⟶ 4,384:
[(prime? x) (printf "~a: ~a\n" (add1 cnt) (comma x))
(loop (* 2 x) (add1 cnt))]
[else (loop (add1 x) cnt)]))</langsyntaxhighlight>
 
{{out}}
Line 3,906 ⟶ 4,438:
The ''best'' way is probably to not use an explicit loop. Just calculate the sequence directly.
 
<syntaxhighlight lang="raku" perl6line># the actual sequence logic
my @seq = grep *.is-prime, (42, { .is-prime ?? $_+<1 !! $_+1 } … *);
 
# display code
say (1+$_).fmt("%-4s"), @seq[$_].flip.comb(3).join(',').flip.fmt("%20s") for ^42;</langsyntaxhighlight>
{{out}}
<pre>1 43
Line 3,956 ⟶ 4,488:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX pgm displays primes found: starting Z at 42, if Z is a prime, add Z, else add 1.*/
numeric digits 20; d=digits() /*ensure enough decimal digits for Z. */
parse arg limit . /*obtain optional arguments from the CL*/
Line 3,975 ⟶ 4,507:
do j=5 by 6 until j*j>#; if # // j==0 | # // (J+2)==0 then return 0
end /*j*/ /* ___ */
return 1 /*Exceeded √ # ? Then # is prime. */</langsyntaxhighlight>
{{out|output}}
<pre>
Line 4,023 ⟶ 4,555:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Loops/Increment loop index within loop body
 
Line 4,037 ⟶ 4,569:
i = i + 1
end
</syntaxhighlight>
</lang>
Output:
<pre>
Line 4,082 ⟶ 4,614:
n = 41 49,752,014,150,467
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, managing the loop index in the stack:
≪ 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}}==
<syntaxhighlight lang="ruby">
<lang Ruby>
require 'prime'
Line 4,100 ⟶ 4,655:
end
end
</syntaxhighlight>
</lang>
Output :
<pre>
Line 4,152 ⟶ 4,707:
So instead we use tail recursion here which, with the use of immutable variables and no side effects, has no such restrictions, and we are save.
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/4HJPkBM/1 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/yzqldLOuRriR6ojqLKaYPQ Scastie (remote JVM)].
<langsyntaxhighlight Scalalang="scala">import scala.annotation.tailrec
 
object LoopIncrementWithinBody extends App {
Line 4,172 ⟶ 4,727:
 
loop(limit, offset)
}</langsyntaxhighlight>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
const func boolean: isPrime (in integer: number) is func
Line 4,205 ⟶ 4,760:
end if;
end for;
end func;</langsyntaxhighlight>
 
{{out}}
Line 4,255 ⟶ 4,810:
=={{header|Smalltalk}}==
{{works with|Smalltalk/X}}
<langsyntaxhighlight lang="smalltalk">numFound := 0.
idx := 42.
[:exit |
Line 4,265 ⟶ 4,820:
numFound == 42 ifTrue:exit
].
] loopWithExit.</langsyntaxhighlight>
Note: above code uses a public domain printf goodie which is part of many Smalltalk's base libraries; if not present in a particular dialect, use regular print/display/transcribe or whatever is available.
{{out}}
Line 4,291 ⟶ 4,846:
 
=={{header|Standard ML}}==
<syntaxhighlight lang="standard ml">
<lang Standard ML>
fun until done change dolast x =
if done x
Line 4,323 ⟶ 4,878:
 
val printp = fn clist:(int*IntInf.int) list =>
List.app (fn i=>print ((Int.toString (#1 i) )^" : "^ (IntInf.toString (#2 i) )^"\n")) clist ; </langsyntaxhighlight>
call <pre>
printp (loop ()) ;
Line 4,368 ⟶ 4,923:
41 : 49752014150467
42 : 99504028301131</pre>
 
=={{header|Tcl}}==
Inspired by Java and Kotlin variants.
 
Tcl allows modifying the loop variable. Everything can be implemented straightforward.
<langsyntaxhighlight lang="tcl">proc isPrime n {
if {[expr $n % 2] == 0} {
return [expr $n == 2]
Line 4,395 ⟶ 4,951:
incr i [expr $i -1]
}
}</langsyntaxhighlight>
{{Out}}
<pre>n=1, i=43
Line 4,444 ⟶ 5,000:
{{trans|Visual Basic .NET}}
{{works with|VBA|VBA Excel 2013}}
<langsyntaxhighlight lang="vb"> Sub Main()
'Loops Increment loop index within loop body - 17/07/2018
Dim imax, i As Integer
Line 4,485 ⟶ 5,041:
Function RightX(c, n)
RightX = Right(Space(n) & c, n)
End Function 'RightX</langsyntaxhighlight>
{{out}}
<pre style="height:60ex">
Line 4,535 ⟶ 5,091:
{{trans|Visual Basic}}
{{works with|Visual Basic .NET|2013}}
<langsyntaxhighlight lang="vbnet">Module LoopsIliwlb
 
Sub Main()
Line 4,576 ⟶ 5,132:
End Function
 
End Module</langsyntaxhighlight>
{{out}}
<pre style="height:60ex">i= 1 : 43
Line 4,620 ⟶ 5,176:
i=41 : 49,752,014,150,467
i=42 : 99,504,028,301,131</pre>
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">fn is_prime(n u64) bool {
if n % 2 == 0 {
return n == 2
}
if n % 3 == 0 {
return n == 3
}
mut d := u64(5)
for d * d <= n {
if n % d == 0 {
return false
}
d += 2
if n % d == 0 {
return false
}
d += 4
}
return true
}
const limit = 42
fn main() {
for i, n := u64(limit), 0; n<limit; i++ {
if is_prime(i) {
n++
println("n = ${n:-2} ${i:19}")
i += i - 1
}
}
}</syntaxhighlight>
 
{{out}}
<pre>
Same as Kotlin entry
</pre>
 
=={{header|Wren}}==
{{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.
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
var isPrime = Fn.new { |n|
Line 4,649 ⟶ 5,243:
}
i = i + 1
}</langsyntaxhighlight>
 
{{out}}
Line 4,695 ⟶ 5,289:
41: 49,752,014,150,467
42: 99,504,028,301,131
</pre>
 
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">i = 42
counter = 0
while counter < 42
if isPrime(i) then
counter = counter + 1
print "n = ", counter, chr$(9), i
i = i + i - 1
end if
i = i + 1
wend
end
 
sub isPrime(v)
if v < 2 return False
if mod(v, 2) = 0 return v = 2
if mod(v, 3) = 0 return v = 3
d = 5
while d * d <= v
if mod(v, d) = 0 then return False else d = d + 2 : fi
wend
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>
 
Line 4,700 ⟶ 5,391:
Uses libGMP (GNU MP Bignum Library) for easy prime detection
rather than write that bit of code and pollute this solution.
<langsyntaxhighlight lang="zkl">var [const] BN=Import("zklBigNum"); // libGMP
n,p := 1,BN(42);
do{
if(p.probablyPrime()){ println("n = %2d %,20d".fmt(n,p)); p.add(p); n+=1; }
p.add(1);
}while(n<=42);</langsyntaxhighlight>
zkl loop variables are iterators that don't allow direct manipulation of
their underlying source. The compiler names these iterators __<index>Walker.
However, by using the look ahead stack, we can keep the iterator from
advancing through the source.
<langsyntaxhighlight lang="zkl">p:=BN(42);
foreach n in ([1..42]){
if(p.probablyPrime()){ println("n = %2d %,20d".fmt(n,p)); p.add(p); }
else{ p.add(1); __nWalker.push(n); } // p not prime, don't advance n
}</langsyntaxhighlight>
{{out}}
<pre style="height:35ex">
1,981

edits