This project contains infinite series generators and visualizations, including:
- An interactive visualizer for number sequences in Rust rust-gui
- A high-performance parallel prime generator in Rust rust-primes,
with reference implementations in Python python-primes and Go golang-primes - An infinite series generator in Rust rust-series
supporting fibonacci, lucas, triangular, collatz stopping times, and powers - A fast parallelized Miller-Rabin primality tester in Rust rust-miller-rabin
| Series | Formula | Sequence |
|---|---|---|
| Primes | Numbers divisible only by 1 and themselves | 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, ... |
| Fibonacci | Fₙ = Fₙ₋₁ + Fₙ₋₂ | 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... |
| Lucas | Lₙ = Lₙ₋₁ + Lₙ₋₂ | 2, 1, 3, 4, 7, 11, 18, 29, 47, 76, ... |
| Triangular | Tₙ = n(n+1)/2 | 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, ... |
| Collatz | Steps to reach 1 via 3n+1 | 0, 0, 1, 7, 2, 5, 8, 16, 3, 19, ... |
| Powers of 2 | 2ⁿ | 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, ... |
| Catalan | Cₙ = (2n)!/(n!(n+1)!) | 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, ... |
| Hexagonal | Hₙ = n(2n-1) | 1, 6, 15, 28, 45, 66, 91, 120, 153, 190, ... |
| Happy | Digit-square sum reaches 1 | 1, 7, 10, 13, 19, 23, 28, 31, 32, 44, ... |
All prime generators are implemented with consistent APIs and comparable algorithms
| Language | Directory | Algorithm | Parallelism |
|---|---|---|---|
| Python | python-primes/ |
Classic/Segmented/Parallel Sieve | multiprocessing.Pool |
| Go | golang-primes/ |
Classic/Segmented/Parallel Sieve | Goroutines + channels |
| Rust | rust-primes/ |
Classic/Segmented/Parallel Sieve | thread::scope |
cd rust-gui
cargo runcd rust-series
cargo run -- -c 100 -s fib # Fibonacci
cargo run -- -c 100 -s lucas # Lucas numbers
cargo run -- -c 100 -s tri # Triangular numbers
cargo run -- -c 100 -s collatz # Collatz stopping times
cargo run -- -c 100 -s pow2 # Powers of 2
cargo run -- -c 100 -s catalan # Catalan numbers
cargo run -- -c 100 -s hex # Hexagonal numbers
cargo run -- -c 100 -s happy # Happy numbersmake benchmark # Run all implementations (1B primes)
make build # Build all
make test # Run all tests
make clean # Clean build artifactscd golang-primes; ./primes 1000 # Go
cd python-primes; python prime_generator.py 1000 # Python
cd rust-primes; ./target/release/primes_cli -n 1000 # Rustcd golang-primes; ./primes --quiet 10000000 # Go
cd python-primes; python prime_generator.py 10000000 --quiet # Python
cd rust-primes; ./target/release/primes_cli -n 10000000 --quiet # Rustcd golang-primes; ./primes --progress 10000000 # Go
cd python-primes; python prime_generator.py 10000000 --progress # Python
cd rust-primes; ./target/release/primes_cli -n 10000000 -P # Rustcd golang-primes; ./primes --parallel --progress 100000000 # Go
cd python-primes; python prime_generator.py 100000000 --parallel --progress # Python
cd rust-primes; ./target/release/primes_cli -n 100000000 -p -P # Rustcd rust-series; cargo run -- -c 100 -s fib # Fibonacci numbers
cd rust-series; cargo run -- -c 100 -s lucas # Lucas numbers
cd rust-series; cargo run -- -c 100 -s tri # Triangular numbers
cd rust-series; cargo run -- -c 100 -s collatz # Collatz stopping times
cd rust-series; cargo run -- -c 100 -s pow2 # Powers of 2
cd rust-series; cargo run -- -c 100 -s catalan # Catalan numbers
cd rust-series; cargo run -- -c 100 -s hex # Hexagonal numbers
cd rust-series; cargo run -- -c 100 -s happy # Happy numbers
cd rust-series; cargo run -- -c 100 -s fib --quiet # Count only
cd rust-series; cargo run -- -c 1000 --progress # With progress barEach implementation auto-selects the best algorithm based on input size:
| n Range | Algorithm | Memory |
|---|---|---|
| n < 1M | Classic Sieve (odd-only) | O(n/2) |
| 1M <= n < 100M | Segmented Sieve (odd-only) | O(sqrt(n) + segment/2) |
| n >= 100M | Parallel Segmented Sieve (odd-only) | O(sqrt(n) + segment/2) per worker |
All three implementations share the same core optimization strategy:
| Optimization | Python | Go | Rust | Impact |
|---|---|---|---|---|
| Odd-only sieve | yes | yes | yes | 2x memory reduction, ~2x faster marking |
| Shared segment helper (DRY) | yes | yes | yes | Single sieving implementation, no duplication |
| SIMD-optimized extraction | bytes.find() |
bytes.IndexByte |
iterator | Hardware-accelerated prime extraction |
| Pre-allocated result vectors | yes | yes | yes | Reduces reallocations |
| Efficient buffer reset | slice assign | for loop | .fill() |
Zero-allocation per segment |
| Delta-based progress callbacks | yes | yes | yes | Correct progress tracking |
| O(n) parallel result merging | heapq.merge |
indexed collect | ordered concat | Avoids O(n log n) sort |
| Shared base primes (no copy) | N/A (pickle) | shared slice | &[usize] ref |
Zero-copy in parallel workers |
| Bounded channel buffers | N/A | numWorkers*2 |
N/A | Limits peak memory in parallel mode |
| Streamed output | generator join | strings.Builder |
BufWriter |
Avoids huge in-memory string |
Python:
bytearraywith slice assignment for fast composite markingmemoryviewfor zero-copy segment operationsmath.isqrt()for exact integer square root (no float imprecision)heapq.merge()for O(n) merging of pre-sorted parallel results- Progress bar writes to stderr (not stdout) to avoid mixing with data
- Returns
List[int](notList[str]) to avoid millions of string allocations
Go:
bytes.IndexBytefor SIMD-optimized prime extraction (replaces hand-rolled linear scan)- Simple
forloop buffer reset (replacesbytes.Repeatwhich allocated per segment) - Atomic counter with monitor goroutine for parallel progress tracking
- Bounded work/result channels to limit memory pressure
- Proper error handling on CLI input parsing
Rust:
thread::scopeallows sharingbase_primesby reference (zero-clone)- Each parallel worker returns a single
Vec<usize>(no intermediateSegmentResultstructs) BufWriterfor streaming output directly to stdout- Correct
format_numberwith comma separators for all magnitudes div_ceil()for idiomatic ceiling division
- Bit-packed storage -- 8x further memory reduction on top of odd-only (16x total vs original)
- Wheel factorization (mod 30) -- skip multiples of 2, 3, 5 to eliminate 73% of candidates
- SIMD composite marking -- pre-computed masks for small primes applied with vectorized AND
- Cache-optimal segment sizing -- auto-tune segment size to fit L1/L2 cache
- Maintained starting offsets -- avoid per-segment division by tracking offsets across segments
See BENCHMARKS.md for detailed benchmark results and analysis.
infinite-series/
├── Makefile # Top-level orchestration
├── AGENTS.md # Guidelines for AI agents
├── README.md # This file
├── python-primes/
│ ├── Makefile
│ ├── prime_generator.py # Core implementation
│ ├── test_generators.py # Test suite (37 tests)
│ ├── performance_comparison.py
│ └── parallel_comparison.py
├── golang-primes/
│ ├── Makefile
│ ├── cmd/primes/main.go # CLI entry point
│ ├── prime/
│ │ ├── primes.go # Core implementation
│ │ ├── primes_test.go # Test suite
│ │ └── primes_benchmark_test.go
│ └── internal/progress/
│ └── progress.go # Progress bar
├── rust-primes/
│ ├── Makefile
│ ├── Cargo.toml
│ └── src/
│ ├── lib.rs # Core implementation
│ ├── primes_cli.rs # CLI entry point
│ └── progress.rs # Progress bar
├── rust-series/
│ ├── Makefile
│ ├── Cargo.toml
│ ├── README.md
│ └── src/
│ ├── lib.rs # Re-exports all series
│ ├── fibonacci.rs # Fibonacci generator
│ ├── lucas.rs # Lucas generator
│ ├── triangular.rs # Triangular generator
│ ├── collatz.rs # Collatz stopping times
│ ├── powers.rs # Powers of 2 generator
│ ├── main.rs # CLI entry point
│ └── progress.rs # Progress bar
├── rust-gui/
│ ├── Makefile
│ ├── Cargo.toml
│ ├── README.md
│ └── src/
│ ├── main.rs # Entry point
│ ├── app.rs # Main application
│ ├── config.rs # Visualization configuration
│ ├── draw_number.rs # Number rendering
│ ├── helpers.rs # Utility constants
│ ├── types.rs # Series and visualization types
│ └── visualizations/ # All visualization implementations
└── rust-miller-rabin/
├── Cargo.toml
└── src/
└── ...
- Requires: Python 3.12+, pytest, ruff (optional), mypy (optional), tqdm (optional)
- Uses
bytearraywith odd-only indexing for memory-efficient sieving - Optional tqdm progress bar with custom ANSI fallback to stderr
--quietflag for count-only output
- Requires: Go 1.21+
- Custom ANSI progress bar (no external dependencies)
- Uses goroutines with bounded work/result channels
- Atomic progress counter with monitor goroutine for parallel mode
- Requires: Rust 1.75+
- rust-primes: CLI for prime generation (
primes_cli) - rust-series: CLI for infinite series (
series_cli) - fibonacci, lucas, triangular, collatz, powers of 2 - rust-gui: Interactive visualizer for all series (
visualizer) - Custom ANSI progress bar with rate display
- Uses
thread::scopefor safe scoped parallelism with zero-copy data sharing

