We’re rewarding the question askers & reputations are being recalculated! Read more.
7 added 339 characters in body
source | link

Go was designed for Google scale. Therefore, Go package testing includes benchmarking tools. For example,

$ go test -bench=. -benchmem
BenchmarkPeterSO-8     500        3805125 ns/op    1007621 B/op     1 allocs/op
BenchmarkNightman-8      1    95703259026 ns/op    5866752 B/op    31 allocs/op
$ 

Go was designed for Google scale. Therefore, Go package testing includes benchmarking tools. For example,

$ go test -bench=. -benchmem
BenchmarkPeterSO-8     500        3805125 ns/op    1007621 B/op     1 allocs/op
BenchmarkNightman-8      1    95703259026 ns/op    5866752 B/op    31 allocs/op
$ 

6 added 95 characters in body
source | link
package main

import (
    "fmt"
    "time"
)

const (
    prime    = 0x00
    notprime = 0xFF
)

func oddPrimes(n uint64) (sieve []uint8) {
    sieve = make([]uint8, (n+1)/2)
    sieve[0] = notprime
    p := uint64(3)
    for i := p * p; i <= n; i = p * p {
        for j := i; j <= n; j += 2 * p {
            sieve[j/2] = notprime
        }
        for p += 2; sieve[p/2] == notprime; p += 2 {
        }
    }
    return sieve
}

func main() {
    start := time.NowsumPrimes()

    var n uint64 = 2000000) -uint64 1{
    sum := uint64(0)
    if n >= 2 {
        sum += 2
    }
    for i, p := range oddPrimes(n) {
        if p == prime {
            sum += 2*uint64(i) + 1
        }
    }
    return sum
}

func main() {
    start := time.Now()

    var n uint64 = 2000000 - 1
    sum := sumPrimes(n)
    fmt.Println(sum)

    fmt.Println(time.Since(start))
}
package main

import (
    "fmt"
    "time"
)

const (
    prime    = 0x00
    notprime = 0xFF
)

func oddPrimes(n uint64) (sieve []uint8) {
    sieve = make([]uint8, (n+1)/2)
    sieve[0] = notprime
    p := uint64(3)
    for i := p * p; i <= n; i = p * p {
        for j := i; j <= n; j += 2 * p {
            sieve[j/2] = notprime
        }
        for p += 2; sieve[p/2] == notprime; p += 2 {
        }
    }
    return sieve
}

func main() {
    start := time.Now()

    var n uint64 = 2000000 - 1
    sum := uint64(0)
    if n >= 2 {
        sum += 2
    }
    for i, p := range oddPrimes(n) {
        if p == prime {
            sum += 2*uint64(i) + 1
        }
    }
    fmt.Println(sum)

    fmt.Println(time.Since(start))
}
package main

import (
    "fmt"
    "time"
)

const (
    prime    = 0x00
    notprime = 0xFF
)

func oddPrimes(n uint64) (sieve []uint8) {
    sieve = make([]uint8, (n+1)/2)
    sieve[0] = notprime
    p := uint64(3)
    for i := p * p; i <= n; i = p * p {
        for j := i; j <= n; j += 2 * p {
            sieve[j/2] = notprime
        }
        for p += 2; sieve[p/2] == notprime; p += 2 {
        }
    }
    return sieve
}

func sumPrimes(n uint64) uint64 {
    sum := uint64(0)
    if n >= 2 {
        sum += 2
    }
    for i, p := range oddPrimes(n) {
        if p == prime {
            sum += 2*uint64(i) + 1
        }
    }
    return sum
}

func main() {
    start := time.Now()

    var n uint64 = 2000000 - 1
    sum := sumPrimes(n)
    fmt.Println(sum)

    fmt.Println(time.Since(start))
}
5 added 41 characters in body
source | link

The algorithm given in the question is much slower than Eratosthenes' well-known algorithm, approximately 25,000 times slower.

The algorithm given in the question is much slower than Eratosthenes' well-known algorithm.

The algorithm given in the question is much slower than Eratosthenes' well-known algorithm, approximately 25,000 times slower.

4 Response to Martin R
source | link
3 edited body
source | link
2 added 5 characters in body
source | link
1
source | link