和果子一起来做题-Project Euler-05-R语言版本

2017-11-23  本文已影响30人  9d760c7ce737

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的最小公倍数
以直觉来结题,对于每一个数判断他是不是能够被1到20的数整除,如果是就保留下来

n=1
while(TRUE){
  if (sum(((n)%%seq(1,20) == 0)) >= 20) break
  n = n +1
}

本质上这个应该能解题,但是算了半天没反应我就只到出问题了
花间一下,这个数首先肯定能被20整除

n=1
while(TRUE){
  if (sum(((20*n)%%seq(1,20) == 0)) >= 20) break
  n = n +1
}

还是算不出来,那应该还要同时被19整除

n=1
while(TRUE){
  if (sum(((380*n)%%seq(1,20) == 0)) >= 20) break
  n = n +1
}

最后算出了答案,380*n=232792560

我看到也可以用分解质数方法来做,但是没有兴趣。

上一篇下一篇

猜你喜欢

热点阅读