Loops/Increment loop index within loop body
Sometimes, one may need (or want) a loop which its iterator (the index variable) is modified within the loop body.
- Goal
Demonstrate the best way to accomplish this.
- Task
Write a loop which:
- starts the index (variable) at 42
- (at iteration time) increments the index by unity
- if the index is prime:
- displays the index and the prime (to the terminal)
- increments the index such that the new index is now that prime
- terminates the loop when 42 primes are shown
Extra credit: because of the primes get rather large, use commas
within the displayed primes to ease comprehension.
Show all output here.
- Note
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.
- Related tasks
- Loop over multiple arrays simultaneously
- Loops/Break
- Loops/Continue
- Loops/Do-while
- Loops/Downward for
- Loops/For
- Loops/For with a specified step
- Loops/Foreach
- Loops/Infinite
- Loops/N plus one half
- Loops/Nested
- Loops/While
Kotlin
Unlike most other 'curly brace' languages, Kotlin's 'for' statement is quite limited and doesn't allow either the iteration variable or the step to be modified within the loop body.
So instead we use a do/while loop here which has no such restrictions. <lang scala>// version 1.2.21
fun isPrime(n: Long): Boolean {
if (n % 2L == 0L) return n == 2L if (n % 3L == 0L) return n == 3L var d = 5L while (d * d <= n) { if (n % d == 0L) return false d += 2L if (n % d == 0L) return false d += 4L } return true
}
fun main(args: Array<String>) {
var i = 42L var n = 0 do { if (isPrime(i)) { n++ System.out.printf("n = %-2d %,19d\n", n, i) i += i - 1 } i++ } while (n < 42)
}</lang>
- Output:
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
REXX
<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*/ if limit== | limit=="," then limit=42 /*Not specified? Then use the default.*/ n=0 /*the count of number of primes found. */
do z=42 until n==limit /* ◄──this DO loop's index is modified.*/ if isPrime(z) then do; n=n + 1 /*Z a prime? Them bump prime counter.*/ say right('n='n, 9) right(commas(z), d) z=z + z - 1 /*also, bump the DO loop index Z. */ end end /*z*/ /* [↑] a small tribute to Douglas Adams*/
exit /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ commas: parse arg _; do j=length(_)-3 to 1 by -3; _=insert(',', _, j); end; return _ /*──────────────────────────────────────────────────────────────────────────────────────*/ isPrime: procedure; parse arg #; if wordpos(#, '2 3 5 7')\==0 then return 1
if # // 2==0 | # // 3 ==0 then return 0 do j=5 by 6 until j*j>#; if # // j==0 | # // (J+2)==0 then return 0 end /*j*/ return 1 /*Exceeded sqrt(#)? Then # is prime.*/</lang>
- output:
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