亚洲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
  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. }
復(fù)制代碼

作者: ba_du_co    時(shí)間: 2015-10-09 14:42
本帖最后由 ba_du_co 于 2015-10-09 14:45 編輯

拿來(lái)練練手。
233168
  1. #!perl6
  2. 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