免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2124 | 回复: 3
打印 上一主题 下一主题

[其他] Project Euler - 006 [复制链接]

论坛徽章:
4
白羊座
日期:2013-11-05 10:26:09冥斗士
日期:2015-11-17 14:19:55白银圣斗士
日期:2015-11-17 15:13:0815-16赛季CBA联赛之新疆
日期:2016-04-01 09:10:58
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-09-27 11:42 |只看该作者 |倒序浏览
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:
前十个自然数的平方和:1^2 + 2^2 + ... + 10^2 = 385
前十个自然数的和的平方是:(1 + 2 + ... + 10) ^ 2 = 55^2 = 3025
所以两者之差是:3025-385=2640
现在,试求出前一百个自然数的和的平方与平方和的差。

代码:

  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. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2015-10-07 15:03 |只看该作者
回复 1# icymirror


    lz没有题目005呀

论坛徽章:
4
白羊座
日期:2013-11-05 10:26:09冥斗士
日期:2015-11-17 14:19:55白银圣斗士
日期:2015-11-17 15:13:0815-16赛季CBA联赛之新疆
日期:2016-04-01 09:10:58
3 [报告]
发表于 2015-10-08 08:17 |只看该作者
回复 2# ba_du_co
有的,但是之前好像都被自动当作重复发帖屏蔽了。

论坛徽章:
0
4 [报告]
发表于 2015-10-09 11:43 |只看该作者
回复 3# icymirror


能重来一发?
谢谢

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP