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

Content added Content deleted
m (→‎{{header|R}}: Syntax highlighting.)
m (→‎{{header|R}}: Improved syntax.)
Line 3,924: Line 3,924:
=={{header|R}}==
=={{header|R}}==
R cannot complete this task with a for loop. See https://stackoverflow.com/a/5913329/ . 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.
R cannot complete this task with a for loop. See https://stackoverflow.com/a/5913329/ . 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 rsplus>i<-42
<lang rsplus>i <- 42
primeCount<-0
primeCount <- 0
while(primeCount<42)
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.
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
primeCount <- primeCount + 1
extraCredit<-format(i, big.mark=",", scientific=FALSE)
extraCredit <- format(i, big.mark=",", scientific = FALSE)
cat("Prime count:",paste0(primeCount,";"),"The prime just found was:",extraCredit,"\n")
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 + i#This is missing the -1 from the Kotlin solution. There is no need to check i + i (it's even).
}
}
i<-i+1
i <- i + 1
}</lang>
}</lang>
{{Out}}
{{Out}}