Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import ("fmt"
- "time"
- )
- func find_primes(max int) []int {
- // list of all primes that we find
- var primes []int
- primes = append(primes, 2)
- var is_prime = true
- for i:=3; i<=max; i++ {
- is_prime = false
- // only odds can be prime
- if i%2 != 0 {
- is_prime = true
- // should not be divisible by any previous
- // prime numbers
- for _, x := range primes {
- if i%x == 0 {
- is_prime = false
- break
- }
- }
- }
- if is_prime {
- primes = append(primes, i)
- }
- }
- return primes
- }
- // just sume up all of the primes
- func sum_primes(primes []int) int {
- var total int = 0
- for _, x := range primes {
- total = total + x
- }
- return total
- }
- func main() {
- start := time.Now()
- // find all primes less than 2,000,000
- primes := find_primes(2000000)
- sum := sum_primes(primes)
- fmt.Println(sum)
- t := time.Now()
- elapsed := t.Sub(start)
- fmt.Println(elapsed)
- }
Add Comment
Please, Sign In to add comment