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

Content added Content deleted
m (→‎{{header|Wren}}: Library name change.)
(Added R solution)
Line 3,418: Line 3,418:
41 -> 49,752,014,150,467
41 -> 49,752,014,150,467
42 -> 99,504,028,301,131</pre>
42 -> 99,504,028,301,131</pre>
=={{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.
<lang R>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
}</lang>
{{Out}}
<pre>Prime count: 1; The prime just found was: 43
Prime count: 2; The prime just found was: 89
Prime count: 3; The prime just found was: 179
Prime count: 4; The prime just found was: 359
Prime count: 5; The prime just found was: 719
Prime count: 6; The prime just found was: 1,439
Prime count: 7; The prime just found was: 2,879
Prime count: 8; The prime just found was: 5,779
Prime count: 9; The prime just found was: 11,579
Prime count: 10; The prime just found was: 23,159
Prime count: 11; The prime just found was: 46,327
Prime count: 12; The prime just found was: 92,657
Prime count: 13; The prime just found was: 185,323
Prime count: 14; The prime just found was: 370,661
Prime count: 15; The prime just found was: 741,337
Prime count: 16; The prime just found was: 1,482,707
Prime count: 17; The prime just found was: 2,965,421
Prime count: 18; The prime just found was: 5,930,887
Prime count: 19; The prime just found was: 11,861,791
Prime count: 20; The prime just found was: 23,723,597
Prime count: 21; The prime just found was: 47,447,201
Prime count: 22; The prime just found was: 94,894,427
Prime count: 23; The prime just found was: 189,788,857
Prime count: 24; The prime just found was: 379,577,741
Prime count: 25; The prime just found was: 759,155,483
Prime count: 26; The prime just found was: 1,518,310,967
Prime count: 27; The prime just found was: 3,036,621,941
Prime count: 28; The prime just found was: 6,073,243,889
Prime count: 29; The prime just found was: 12,146,487,779
Prime count: 30; The prime just found was: 24,292,975,649
Prime count: 31; The prime just found was: 48,585,951,311
Prime count: 32; The prime just found was: 97,171,902,629
Prime count: 33; The prime just found was: 194,343,805,267
Prime count: 34; The prime just found was: 388,687,610,539
Prime count: 35; The prime just found was: 777,375,221,081
Prime count: 36; The prime just found was: 1,554,750,442,183
Prime count: 37; The prime just found was: 3,109,500,884,389
Prime count: 38; The prime just found was: 6,219,001,768,781
Prime count: 39; The prime just found was: 12,438,003,537,571
Prime count: 40; The prime just found was: 24,876,007,075,181
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}}==
=={{header|Racket}}==