How to resolve the algorithm Loops/Increment loop index within loop body step by step in the J programming language

Published on 12 May 2024 09:40 PM
#J

How to resolve the algorithm Loops/Increment loop index within loop body step by step in the J programming language

Table of Contents

Problem Statement

Sometimes, one may need   (or want)   a loop which its   iterator   (the index variable)   is modified within the loop body   in addition to the normal incrementation by the   (do)   loop structure index.

Demonstrate the best way to accomplish this.

Write a loop which:

Extra credit:   because of the primes get rather large, use commas within the displayed primes to ease comprehension.

Show all output here.

Not all programming languages allow the modification of a loop's index.   If that is the case, then use whatever method that is appropriate or idiomatic for that language.   Please add a note if the loop's index isn't modifiable.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Loops/Increment loop index within loop body step by step in the J programming language

Source code in the j programming language

   (,.~#\)}:(}:, (,1&p: # _1 2&p.)@:>:@{:)^:(42 >: #)^:_ x: 42
 1             43
 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


isPrime =: 1&p:
assert 1 1 0 -: isPrime 2 3 4   NB. test and example

loop =: verb define
 i =. x: y
 n =. i. 0
 while. y > # n do.
  if. isPrime i do.
   n =. n , i
   i =. _1 2 p. i
  end.
  i =. i + 1
 end.
 n 
)


loop =: verb define@:x:
 i =. y
 while. y >: # i do.
  if. isPrime {: i do.
   i =. (, _1 2 p. {:) i
  end.
  i =. _1 (>:@:{)`[`]} i
 end.
 }: i
)


loop =: verb define@:x:
 i =. y
 while. y >: # i do.
  i =. (, (isPrime # _1 2&p.)@:{:) i
  i =. _1 (>:@:{)`[`]} i
 end.
 }: i
)


save_if_prime =: , (isPrime # _1 2&p.)@:{:
increment_tail =: _1&(>:@:{`[`]})

loop =: verb define@:x:
 i =. y
 while. y >: # i do.
  i =. save_if_prime i
  i =. increment_tail i
 end.
 }: i
)


loop =: verb define@:x:
 i =. y
 while. y >: # i do.
  i =. increment_tail@:save_if_prime i
 end.
 }: i
)


While =: conjunction def 'u^:(0~:v)^:_'

loop =: verb define@:x:
 i =. y
 }: increment_tail@:save_if_prime While(y >: #) i
)


isPrime =: 1&p:
save_if_prime =: , (isPrime # _1 2&p.)@:{:
increment_tail =: _1&(>:@:{`[`]})
While =: conjunction def 'u^:(0~:v)^:_'
tacit_loop =: [: }: (increment_tail@:save_if_prime@:]While(>: #) x:)


   9!:37 ] 0 2048 0 222  NB. output control permit lines of 2^11 columns

   (>:@:i. ,: tacit_loop) 42
 1  2   3   4   5    6    7    8     9    10    11    12     13     14     15      16      17      18       19       20       21       22        23        24        25         26         27         28          29          30          31          32           33           34           35            36            37            38             39             40             41             42
43 89 179 359 719 1439 2879 5779 11579 23159 46327 92657 185323 370661 741337 1482707 2965421 5930887 11861791 23723597 47447201 94894427 189788857 379577741 759155483 1518310967 3036621941 6073243889 12146487779 24292975649 48585951311 97171902629 194343805267 388687610539 777375221081 1554750442183 3109500884389 6219001768781 12438003537571 24876007075181 49752014150467 99504028301131
   
   
   NB. fix the definition.  Here's the code.
   tacit_loop f.
[: }: (_1&(>:@:{`[`]})@:(, (1&p: # _1 2&p.)@:{:)@:]^:(0 ~: (>: #))^:_ x:)


   extra_credit =: ([: }. ,@(',' ,.~ _3 [\ ])&.|.@:":)&>
   show =: [ ([: echo@:deb@:({. , ' ' , {:)@:extra_credit # , {:)
   save_if_prime =: (, _1 2&p.@:{:)@:show^:(isPrime@:{:)
   empty@:tacit_loop 42
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


  

You may also check:How to resolve the algorithm File size step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Take notes on the command line step by step in the Swift programming language
You may also check:How to resolve the algorithm Department numbers step by step in the Mercury programming language
You may also check:How to resolve the algorithm Sum multiples of 3 and 5 step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Stack step by step in the Oberon-2 programming language