Problem 39 もっかい

Problem 9の回答見ながら直したら早くなった。

gcd <- function(a, b){
  while(a%%b){
    a <- a%%b
    temp <- a
    a <- b
    b <- temp
  }
  b
}

limit <- 499
max.count <- 0
ans <- 0

for(i in 1:limit){
  count <- 0
  mlim <- sqrt(i)+1
  for(m in seq(1, mlim, by=2)){
    k <- i/m
    if(gcd(m,k)==1){
      count <- count+1
    }
  }
  if(count > max.count){
    max.count <- count
    ans <- i*2
  }
}