-
Notifications
You must be signed in to change notification settings - Fork 8
/
solution.go
67 lines (59 loc) · 1.3 KB
/
solution.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"fmt"
"math"
"time"
)
func IsPrime(number int) bool {
if number == 2 {
return true
}
square_root := int(math.Floor(math.Sqrt(float64(number))))
for i := 2; i <= square_root; i++ {
if number % i == 0 {
return false
}
}
return true
}
func SieveOfEratosthenes(N int) (primes []int) {
b := make([]bool, N)
for i := 2; i < N; i++ {
if b[i] == true { continue }
primes = append(primes, i)
for k := i * i; k < N; k += i {
b[k] = true
}
}
return
}
func SumFirstTwoMillionPrimeNumbers() (sum int) {
sum = 0
for num := 2; num < 2000000; num++ {
if IsPrime(num) {
sum += num
}
}
return
}
func SumFirstTwoMillionPrimeNumbers2() (sum int) {
sum = 0
for _, p := range SieveOfEratosthenes(2000000) {
sum += p
}
return
}
func main() {
start := time.Now()
result := SumFirstTwoMillionPrimeNumbers()
elapsed := time.Since(start)
fmt.Println("=> Solution 1")
fmt.Printf("=> Result: %d\n", result)
fmt.Printf("=> Time: %.8fs\n", elapsed.Seconds())
start2 := time.Now()
result2 := SumFirstTwoMillionPrimeNumbers2()
elapsed2 := time.Since(start2)
fmt.Println("=> Solution 2")
fmt.Printf("=> Result: %d\n", result2)
fmt.Printf("=> Time: %.8fs\n", elapsed2.Seconds())
}