PE10 Summation of primes

Summation of primes

Problem 10

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

package main

import (
    "fmt"
    "math"
)

func main() {
    sum := 0
    for i := 1; i < 2000000; i++ {
        if Prime1(i) {
            sum += i
        }
    }
    fmt.Println(sum - 1)
}
func Prime1(num int) bool {
    if num == 2 || num == 3 {
        return true
    }
    if num%6 != 1 && num%6 != 5 {
        return false
    }
    tmp := int(math.Sqrt(float64(num)))
    for i := 5; i <= tmp; i += 6 {
        if num%i == 0 || num%(i+2) == 0 {
            return false
        }
    }
    return true
}

结果:142913828922

posted @ 2018-12-09 20:27  喜欢沧月的二福君  阅读(172)  评论(0编辑  收藏  举报

联系博主qq2456891380