If you write the R code using vectorised operations it’s significant orders of magnitude faster (148 seconds to 1.5 milliseconds on posit.cloud).
The nested loop becomes: j <- sum((seq_len(100000) - 1) %% u) a <- a + j + r