Files
codekata-golang/10_summation_of_primes/main.go
2020-10-22 14:04:57 +08:00

41 lines
493 B
Go

package main
import "fmt"
func main() {
sum := 0
from := 2
count := 0
for {
if from == 2000000 {
break
}
if IsPrime(from) {
fmt.Println("prime:", from)
sum += from
count++
}
from++
}
fmt.Println("sum:", sum, count)
}
func IsPrime(n int) bool {
if n < 2 {
return false
}
if n == 2 {
return true
}
squareRoot := 1
for squareRoot*squareRoot < n {
squareRoot++
}
for i := 2; i <= squareRoot; i++ {
if n%i == 0 {
return false
}
}
return true
}