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

2018-01-28  本文已影响10人  9d760c7ce737

这是第六题:

The sum of the squares of the first ten natural numbers is,
12+ 22+ ... + 102 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)22 = 552 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

审题:
求出1到100的平方和
再求出1到100和的平方
求出二者只差

看起来很难,但是做起来却很简单

j <- 0
k <- 0
for (i in 1:100) {
  j = j + i^2
  k = k +i
}
dif <- k^2 -j
dif

最终结果是
25164150

上一篇下一篇

猜你喜欢

热点阅读