/
isprime.go
58 lines (46 loc) · 1.12 KB
/
isprime.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
package main
import (
"flag"
"fmt"
"math"
"strconv"
"os"
)
// This function will eventually get it's own module.
func isPrime(t int64) bool {
// math.Mod requires floats.
x := float64(t)
// 1 or less aren't primes.
if x <= 1 {
return false
}
// Solve half of the integer set directly
if math.Mod(x, 2) == 0 {
return x == 2
}
// Main loop. i needs to be float because of math.Mod.
for i := 3.0; i <= math.Floor(math.Sqrt(x)); i += 2.0 {
if math.Mod(x, i) == 0 {
return false
}
}
// It's a prime!
return true
}
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: %s int\n", os.Args[0])
flag.PrintDefaults()
os.Exit(2)
}
flag.Parse()
args := flag.Args()
if len(args) < 1 || len(args) > 1 {
flag.Usage()
}
// I really shouldn't omit the error message here...
x, _ := strconv.ParseInt(args[0], 0, 64)
// Should i os.exit(isPrime())?
// It's more logical to do so, but not convenient for most shell users.
fmt.Println(isPrime(x))
}