mirror of
https://github.com/tiennm99/prime-generator.git
synced 2026-10-11 03:13:44 +00:00
58 lines
1.4 KiB
Markdown
58 lines
1.4 KiB
Markdown
# prime-generator
|
|
|
|
Generates every prime from 0 up to a limit with the sieve of Eratosthenes and
|
|
writes them one per line to a text file. The default limit is 1,000,000,000.
|
|
|
|
*Originally written in Java. In 2026 it was rewritten in Go; the Java version is
|
|
on the [feature/java](https://github.com/tiennm99/prime-generator/tree/feature/java)
|
|
branch.*
|
|
|
|
## Requirements
|
|
|
|
- Go 1.25 or higher
|
|
- Disk space for the output — a full run to 1,000,000,000 writes 50,847,534
|
|
primes, roughly 500 MB
|
|
- About 1 GB of RAM at the default limit, since the sieve holds one byte per
|
|
number
|
|
|
|
## Usage
|
|
|
|
```sh
|
|
go run .
|
|
```
|
|
|
|
Or build a binary first:
|
|
|
|
```sh
|
|
go build -o prime-generator .
|
|
./prime-generator
|
|
```
|
|
|
|
### Flags
|
|
|
|
| Flag | Default | Purpose |
|
|
| --- | --- | --- |
|
|
| `-n` | `1000000000` | Upper limit, inclusive, to sieve |
|
|
| `-out` | `primes.txt` | Output file path |
|
|
|
|
```sh
|
|
# Primes below 100, written somewhere else
|
|
./prime-generator -n 100 -out small.txt
|
|
```
|
|
|
|
## Tests
|
|
|
|
```sh
|
|
go test ./...
|
|
```
|
|
|
|
The sieve is checked against an independent trial-division implementation, and
|
|
against the published prime counts at powers of ten up to 1,000,000 — so a bug
|
|
in the sieve is not mirrored by the thing checking it.
|
|
|
|
## Related
|
|
|
|
[crawl-prime](https://github.com/tiennm99/crawl-prime) reaches the same output
|
|
file from the other direction: it downloads a published prime list instead of
|
|
computing one.
|