-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathulam.go
98 lines (78 loc) · 1.9 KB
/
ulam.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// ulam.go
// Usage ./ulam -r "resolution scale" -o "output file name"
package main
import(
"fmt"
"flag"
"image"
"image/color"
"image/png"
"image/draw"
"os"
"log"
)
func isPrime(n int) bool {
if n == 2 {
return true
}
if n == 3 {
return true
}
if n%2 == 0 {
return false
}
if n%3 == 0 {
return false
}
i := 5
w := 2
for i*i <= n {
if n%i == 0 {
return false
}
i += w
w = 6 - w
}
return true
}
func main() {
// Initialize flags for command line
out := flag.String("o", "out", "Output file name (without png extention)")
res := flag.Int("r", 800, "Resolution (width), 800 gives an image of 800*800")
flag.Parse()
width := *res
// Array to store the list of primes
a := make([]bool, width*width)
for c:= range a {
a[c] = isPrime(c)
}
// Open up an empty image
img := image.NewRGBA(image.Rect(0,0,width,width))
// Draw the white background
draw.Draw(img, img.Bounds(), &image.Uniform{color.RGBA{255, 255, 255, 255}}, image.ZP, draw.Src)
// Set the color to draw the dots for primes
col := color.RGBA{0, 0, 0, 255}
// Begin generating spiral
var w int = 0
var h int = 0
var dh int = -1
var dw int = 0
for k := range a {
if (-width/2 <= w) && (w <= width/2) && (-width/2 <= h) && (h <= width/2) {
if(a[k]){
img.Set(w+(width/2), h+(width/2), col)
}
}
if ( w == h) || ( w<0 && w == -h ) || (w>0 && w == 1-h) {
dw, dh = -dh, dw
}
w += dw
h += dh
}
outf, err := os.Create(fmt.Sprintf(*out+".png"))
if err != nil {
log.Fatalln(err)
}
defer outf.Close()
png.Encode(outf, img)
}