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

Content added Content deleted
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
(J: bugfix (the initial implementation didn't quite match the requirement for display). Note that some of this content is still unreviewed)
Line 2,457: Line 2,457:


=={{header|J}}==
=={{header|J}}==
An idiomatic approach:
Fun with j. The verb tacit_loop implements the computation.
<syntaxhighlight lang="j">
<syntaxhighlight lang=J> (,.~#\)}:(}:, (,1&p: # _1 2&p.)@:>:@{:)^:(42 >: #)^:_ x: 42
1 43
tacit_loop =: _1&(>:@:{`[`]})@:(, (1&p: # _1 2&p.)@:{:)@:]^:(0 ~: (>: #))^:_ x:
2 89
3 179
4 359
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>
</syntaxhighlight>

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.
A variant derived from the python solution (except this loop returns the list of computed values rather than displays them):
<syntaxhighlight lang="j">
<syntaxhighlight lang="j">
isPrime =: 1&p:
isPrime =: 1&p: