Project Euler5

Problem5

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 positive number that is evenly divisible by all of the numbers from 1 to 20?

1から20までの全ての数で割り切れる数のうち最小の正の数を求めよという問題。

#1から20までの範囲で素数を求める
prime_num <- 2
for(i in 3:20){
	if(any(i%%prime_num==0)){
		next}else{
		prime_num <- c(prime_num, i)
		}
}

#求めた素数をかける
res0 <- eval(parse(text = paste(collapse="*", prime_num)))

#素数をN倍して1から20で割るという操作を繰り返す
j <- 2
repeat{
	res <- res0 * j
	if(sum(res %% 1:20)==0){
		break
	}else{
		j <- j + 1
	}
}
res