Using Chris' logic only to test the set of 9699690, I can find the answer with:
found <- FALSE test <- 9699690 while(!found) { test <- test + 9699690 found <- all(test%%(1:20)==0) } cat("The number is: ",test,"\n") The number is: 232792560
Edit:
As for why OPs code doesn't work, you are most likely interested in, and not solving, this little riddle, there is only one small problem with the code, and that is probably not what you are doing. If we enter one value before the true answer:
a = 232792559 c = 0 while ( c < 20){ c = 0 n = 1 while ( n < 21 ){ if (a%%n == 0) c = c + 1 n = n+1 } a = a + 1 } print (a) [1] 232792561
We get too much because you add 1 to a , even if the answer is correct. Move it to the front of the outer loop and it works. It just seems like an infinite loop, because in R it slows down slowly to figure things out. In R, we need to do something different than languages ββlike C, because the R code does not compile (which means the loop takes a lot of time) and is optimized for vectorized input.
Looking at your code, I will tell you that you are new to R, but perhaps experienced in other languages, but this experience will not help you with this. I would advise you to read about how to vectorize things.
source share