-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path543.go
53 lines (46 loc) · 798 Bytes
/
543.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
// UVa 543 - Goldbach's Conjecture
package main
import (
"fmt"
"io"
"os"
)
func isPrime(n int) bool {
for i := 3; i*i <= n; i += 2 {
if n%i == 0 {
return false
}
}
return true
}
var primeSet = func() map[int]bool {
primeSet := make(map[int]bool)
for i := 3; i < 1000000; i += 2 {
if isPrime(i) {
primeSet[i] = true
}
}
return primeSet
}()
func solve(out io.Writer, n int) {
for i := 3; i <= n/2; i += 2 {
if primeSet[i] && primeSet[n-i] {
fmt.Fprintf(out, "%d = %d + %d\n", n, i, n-i)
return
}
}
fmt.Fprintln(out, "Goldbach's conjecture is wrong.")
}
func main() {
in, _ := os.Open("543.in")
defer in.Close()
out, _ := os.Create("543.out")
defer out.Close()
var n int
for {
if fmt.Fscanf(in, "%d", &n); n == 0 {
break
}
solve(out, n)
}
}