Skip to content

Commit cc9dc7c

Browse files
committed
Initial commit
0 parents  commit cc9dc7c

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
a.out*
2+
primes
3+
primes.exe
4+
primes.txt

LICENSE

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2012 Martino di Filippo <[email protected]>
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
Primes.go
2+
====
3+
4+
### Introduction
5+
6+
A quick test implementing the Sieve of Eratostenes in Go.
7+
8+
It searches for prime numbers up to the specified limit, optionally saving them to file.
9+
10+
It is one of many small programs I wrote to get a feel for [#golang](http://golang.org/). If you're interested in generating prime numbers, you should probably look into [primegen](http://cr.yp.to/primegen.html) or some other, more suitable alternative.
11+
12+
### Compiling
13+
14+
$ go build primes.go
15+
16+
### Usage
17+
18+
primes [-l limit] [-s]
19+
20+
* -l limit *(default 100000)* Upper limit for prime searching
21+
* -s *(default false)* Save the prime numbers to primes.txt
22+
23+
### Speed
24+
25+
These are some non-accurate benchmarks I ran on my machine.
26+
27+
**Specs**
28+
29+
* AMD Phenom II X6 1055T @3.4GHz
30+
* 8GB of cheap Kingston DDR3 @486MHz
31+
* Windows 8 RTM 64-bit
32+
33+
**Results**
34+
35+
Ram usage and CPU time for finding primes up to:
36+
37+
1'000'000 <1MB 2ms
38+
10'000'000 <1MB 26ms
39+
100'000'000 7.4MB 401ms
40+
1'000'000'000 61.2MB 9.01s
41+
10'000'000'000 599.8MB 1m51s
42+
43+
### License
44+
45+
Copyright 2012 Martino di Filippo
46+
47+
Licensed under the MIT License

primes.go

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
"time"
9+
)
10+
11+
func allocate(limit int64) []uint64 {
12+
bits := uint64((limit / 2) - 1)
13+
mod := bits % 64
14+
ints := (bits - mod) / 64 + 1
15+
16+
primes := make([]uint64, ints)
17+
18+
if mod != 0 {
19+
// if the last bitmask isn't complete, set all remaining bits to 1
20+
primes[ints - 1] |= (0xFFFFFFFFFFFFFFFF << mod)
21+
}
22+
23+
return primes
24+
}
25+
26+
func sieve(limit uint64, primes []uint64) {
27+
fmt.Printf("Finding all primes <= %v\n", limit)
28+
29+
start := time.Now()
30+
31+
for k, i, run := uint64(0), uint64(3), true; run; k++ {
32+
for l := uint8(0); l < 64; l, i = l + 1, i + 2 {
33+
if (primes[k] >> l) & 1 == 1 {
34+
// number was already marked as composite
35+
continue
36+
}
37+
38+
sqr := i * i
39+
if sqr > limit {
40+
run = false
41+
break
42+
}
43+
44+
for d := i * 2; sqr <= limit; sqr += d {
45+
// mark all odd multiples from i*i to limit as composites
46+
p := uint64((sqr - 3) / 2)
47+
primes[p / 64] |= (1 << (p % 64))
48+
}
49+
}
50+
}
51+
52+
fmt.Printf("Done in %v.\n", time.Since(start))
53+
}
54+
55+
func save(primes []uint64) {
56+
fmt.Printf("\nSaving to primes.txt...\n");
57+
ints := uint(len(primes))
58+
start := time.Now()
59+
60+
file, err := os.Create("primes.txt")
61+
if err != nil { panic(err) }
62+
defer file.Close()
63+
64+
fmt.Fprintln(file, 2)
65+
for k := uint(0); k < ints; k++ {
66+
for l := uint(0); l < 64; l++ {
67+
if (primes[k] >> l) & 1 == 0 {
68+
fmt.Fprintln(file, (k * 64 + l) * 2 + 3)
69+
}
70+
}
71+
}
72+
73+
fmt.Printf("Done in %v.\n", time.Since(start))
74+
}
75+
76+
func main() {
77+
limit := flag.Int64("l", 100000, "Upper limit for prime searching")
78+
write := flag.Bool("s", false, "Save the prime numbers to primes.txt")
79+
80+
flag.Usage = func() {
81+
fmt.Fprintln(os.Stderr, "A quick test implementing the Sieve of Eratostenes in Go")
82+
fmt.Fprintln(os.Stderr, "It searches for prime numbers up to the specified limit\n")
83+
fmt.Fprintf(os.Stderr, "Usage: %s [-l limit] [-s]\n\n", filepath.Base(os.Args[0]))
84+
flag.PrintDefaults()
85+
fmt.Fprint(os.Stderr, "\n")
86+
}
87+
88+
flag.Parse()
89+
90+
primes := allocate(*limit)
91+
sieve(uint64(*limit), primes)
92+
93+
if *write {
94+
save(primes)
95+
}
96+
}

0 commit comments

Comments
 (0)