美文网首页
和果子一起来做题-Project Euler-06-R语言版本

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

作者: 9d760c7ce737 | 来源:发表于2018-01-28 18:21 被阅读10次

    这是第六题:

    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

    相关文章

      网友评论

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

          本文链接:https://www.haomeiwen.com/subject/loszaxtx.html