Skip to content

Instantly share code, notes, and snippets.

@gregce
Created March 7, 2017 21:17
Show Gist options
  • Select an option

  • Save gregce/d7d5d82c5d1d8d423fe6112e00dbf828 to your computer and use it in GitHub Desktop.

Select an option

Save gregce/d7d5d82c5d1d8d423fe6112e00dbf828 to your computer and use it in GitHub Desktop.
library(tictoc)
smallest_multiple2 <- function(x, top_range=10, ...) {
for (div in seq(1:top_range)) {
if (x %% div != 0) return(FALSE)
}
return(TRUE)
}
tic()
test= FALSE
init <- 0
while(test == FALSE) {
init = init + 20
if (smallest_multiple2(init,20) == TRUE) break
}
toc()
#slower solution
# smallest_multiple <- function(x, top_range=10, ...) {
# var = vector()
# for (div in seq(1:top_range)) {
# var = c(var, x %% div)
# }
# return(var)
# }
#
#
# while(test == FALSE) {
# init = init + 10
# if (sum(smallest_multiple(init,10)) == 0) {
# print(paste0("Smallest multiple is:", init))
# test <<- TRUE
# }
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment