Created
March 7, 2017 21:17
-
-
Save gregce/d7d5d82c5d1d8d423fe6112e00dbf828 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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