亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: Project Euler - 006 [打印本頁]

作者: icymirror    時間: 2015-09-27 11:42
標(biāo)題: Project Euler - 006
Problem 006:
The sum of the squares of the first ten natural numbers is,
1^2 + 2^2 + ... + 10^2 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)^2 = 55^2 = 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.

問題6:
前十個自然數(shù)的平方和:1^2 + 2^2 + ... + 10^2 = 385
前十個自然數(shù)的和的平方是:(1 + 2 + ... + 10) ^ 2 = 55^2 = 3025
所以兩者之差是:3025-385=2640
現(xiàn)在,試求出前一百個自然數(shù)的和的平方與平方和的差。

代碼:

  1. package main

  2. import (
  3.         "fmt"
  4. )

  5. func SumOfArithmeticSequence(min, max, step int) int {
  6.         return (min + max) * ((max - min + 1) / step) / 2
  7. }

  8. func SumOfSquareSequence(min, max int) int {
  9.         result := 0
  10.         for index := min; index <= max; index++ {
  11.                 result = result + index * index
  12.         }
  13.         return result
  14. }

  15. func Problem006(maxNumber int) int {
  16.     square := SumOfSquareSequence(1, maxNumber)   
  17.         sequence := SumOfArithmeticSequence(1, maxNumber, 1)
  18.         return sequence * sequence - square
  19. }

  20. func main() {
  21.         fmt.Println("Problem 006 result: ", Problem006(100))
  22. }
復(fù)制代碼

作者: ba_du_co    時間: 2015-10-07 15:03
回復(fù) 1# icymirror


    lz沒有題目005呀
作者: icymirror    時間: 2015-10-08 08:17
回復(fù) 2# ba_du_co
有的,但是之前好像都被自動當(dāng)作重復(fù)發(fā)帖屏蔽了。
作者: ba_du_co    時間: 2015-10-09 11:43
回復(fù) 3# icymirror


能重來一發(fā)?
謝謝






歡迎光臨 Chinaunix (http://72891.cn/) Powered by Discuz! X3.2