Project Euler Problem 5

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?

単なる最小公倍数なのですぐにやり方はわかるのに,プログラムの書き方で悩む.青木先生のユークリッドの互除法による最小公倍数のコードをパクリ,繰り返しで求めた.

eucrid <- function(m, n){
          limit <- 2^52
          stopifnot(is.numeric(m) && is.numeric(n) 
                    && m == floor(m) && n == floor(n) 
                    && m < limit && n < limit)
          m0 <- m <- abs(m)
          n0 <- n <- abs(n)
          while ((temp <- n %% m) != 0) {
                  n <- m
                  m <- temp
          }
          lcm <- (m0/m)*n0
          return(list(GCM=m, quotient=c(m0/m, n0/m), 
                 LCM=ifelse(lcm > limit, NA, lcm)))
  }
p <- 1
for (i in 2:20){
  p <- eucrid(p,i)$LCM
}
p