亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区
Chinaunix
標(biāo)題:
Project Euler - 001
[打印本頁(yè)]
作者:
icymirror
時(shí)間:
2015-09-24 23:05
標(biāo)題:
Project Euler - 001
本帖最后由 icymirror 于 2015-09-26 17:34 編輯
最近沒(méi)有那么忙,開始學(xué)習(xí)golang,有空就拿Project Euler上的題目來(lái)練手吧。
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.
問(wèn)題1:
如果我們把10以內(nèi)的3或5的倍數(shù)列出來(lái),我們會(huì)得到:3, 5, 6和9。這些數(shù)之和是23。
現(xiàn)在,找出1000以內(nèi)的3或5的倍數(shù)的數(shù)字之和。
method 1
package main
import (
"fmt"
)
func Problem001_basic(scope, factor1, factor2 int) int {
sum := 0
for index := 0; index <= scope; index += factor1 {
sum = sum + index
}
for index := 0; index <= scope; index += factor2 {
sum = sum + index
}
for index := 0; index <= scope; index += factor1 * factor2 {
sum = sum - index
}
return sum
}
func Problem001_improve(scope, factor1, factor2 int) int {
sum := (factor1 + scope / factor1 * factor1) * (scope / factor1) / 2
sum = sum + (factor2 + scope / factor2 * factor2) * (scope / factor2) / 2
factor3 := factor1 * factor2
sum = sum - (factor3 + scope / factor3 * factor3) * (scope / factor3) / 2
return sum
}
func main() {
fmt.Println("Problem 001 result: ", Problem001_basic(1000, 3, 5))
fmt.Println("Problem 001 result: ", Problem001_improve(1000, 3, 5))
}
復(fù)制代碼
作者:
ba_du_co
時(shí)間:
2015-10-09 14:42
本帖最后由 ba_du_co 于 2015-10-09 14:45 編輯
拿來(lái)練練手。
233168
#!perl6
say [+] (^1000).grep: * %% (3|5);
復(fù)制代碼
作者:
zsszss0000
時(shí)間:
2016-04-21 15:45
結(jié)果是 234168吧 !
回復(fù)
2#
ba_du_co
歡迎光臨 Chinaunix (http://www.72891.cn/)
Powered by Discuz! X3.2