免费注册 查看新帖 |

Chinaunix

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

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

论坛徽章:
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-24 23:05 |只看该作者 |倒序浏览
本帖最后由 icymirror 于 2015-09-26 17:34 编辑

最近没有那么忙,开始学习golang,有空就拿Project Euler上的题目来练手吧。

Problem 1.
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.

问题1:
如果我们把10以内的3或5的倍数列出来,我们会得到:3, 5, 6和9。这些数之和是23。
现在,找出1000以内的3或5的倍数的数字之和。

method 1
  1. package main

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

  5. func Problem001_basic(scope, factor1, factor2 int) int {
  6.         sum := 0

  7.         for index := 0; index <= scope; index += factor1 {
  8.                 sum = sum + index
  9.         }
  10.        
  11.         for index := 0; index <= scope; index += factor2 {
  12.                 sum = sum + index
  13.         }
  14.        
  15.         for index := 0; index <= scope; index += factor1 * factor2 {
  16.                 sum = sum - index
  17.         }
  18.        
  19.         return sum
  20. }

  21. func Problem001_improve(scope, factor1, factor2 int) int {
  22.         sum := (factor1 + scope / factor1 * factor1) * (scope / factor1) / 2
  23.         sum = sum + (factor2 + scope / factor2 * factor2) * (scope / factor2) / 2
  24.         factor3 := factor1 * factor2
  25.         sum = sum -  (factor3 + scope / factor3 * factor3) * (scope / factor3) / 2
  26.         return sum
  27. }

  28. func main() {
  29.         fmt.Println("Problem 001 result: ", Problem001_basic(1000, 3, 5))
  30.         fmt.Println("Problem 001 result: ", Problem001_improve(1000, 3, 5))
  31. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2015-10-09 14:42 |只看该作者
本帖最后由 ba_du_co 于 2015-10-09 14:45 编辑

拿来练练手。
233168
  1. #!perl6
  2. say [+] (^1000).grep: * %% (3|5);
复制代码

求职 : 机器学习
论坛徽章:
79
2015年亚洲杯纪念徽章
日期:2015-05-06 19:18:572015七夕节徽章
日期:2015-08-21 11:06:172015亚冠之阿尔纳斯尔
日期:2015-09-07 09:30:232015亚冠之萨济拖拉机
日期:2015-10-21 08:26:3915-16赛季CBA联赛之浙江
日期:2015-12-30 09:59:1815-16赛季CBA联赛之浙江
日期:2016-01-10 12:35:21技术图书徽章
日期:2016-01-15 11:07:2015-16赛季CBA联赛之新疆
日期:2016-02-24 13:46:0215-16赛季CBA联赛之吉林
日期:2016-06-26 01:07:172015-2016NBA季后赛纪念章
日期:2016-06-28 17:44:45黑曼巴
日期:2016-06-28 17:44:4515-16赛季CBA联赛之浙江
日期:2017-07-18 13:41:54
3 [报告]
发表于 2016-04-21 15:45 |只看该作者
结果是 234168吧  !回复 2# ba_du_co


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

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP