How to find the sum of all the primes below 10000 (ten thousand) in Go

1 Answer

0 votes
package main

import (
    "fmt"
    "math"
)

func isPrime(n int) bool {
    if n < 2 || (n%2 == 0 && n != 2) {
        return false
    }

    count := int(math.Floor(math.Sqrt(float64(n))))
    for i := 3; i <= count; i += 2 {
        if n%i == 0 {
            return false
        }
    }

    return true
}

func main() {
    num := 10000
    sum := 0

    for i := 2; i < num; i++ {
        if isPrime(i) {
            sum += i
        }
    }

    fmt.Printf("sum = %d\n", sum)
}



/*
run:

sum = 5736396

*/

 



answered Jul 26, 2025 by avibootz
...