-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem10.go
More file actions
58 lines (46 loc) · 729 Bytes
/
problem10.go
File metadata and controls
58 lines (46 loc) · 729 Bytes
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
package main
import (
"fmt"
"strconv"
)
func IsPrime(x int)(ret bool){
// corner cases
if x <= 1 {
return false
}
if x == 2 || x == 3 {
return true
}
// below 5 cases for 2 and 3
if x % 2 == 0 || x % 3 == 0 {
return false
}
for i := 5; i * i <= x; i += 6 {
if x % i == 0 || x % ( i + 2 ) == 0 {
return false
}
}
return true
}
func nthPrime(x int)(ret int){
a := 2
y := x
for y > 0{
if IsPrime(a){
y--
}
a++
}
a--
return a
}
func main() {
// repurpose problem 7 code to solve problem 10: summation of all primes under 2 000 000
var sum int
for x:=0;x<2000000;x++{
if IsPrime(x){
sum += x
}
}
fmt.Println("Sum of all primes under 2 million: " + strconv.Itoa(sum))
}