This repository contains a Rust implementation of various primality tests, which powers a command-line utility for computing and visualizing prime sequences.
The distribution of primes and related numbers is not random, and certain properties of the corresponding sequences can be visualized using graphs such as the Ulam Spiral. Here is an example marking the prime numbers from 1 to 1048576 in a spiral arrangement on a square lattice:
Recurring patterns emerge in the visualization, notably lines and intersections. This software package provides ways to perform experimental mathematics on this topic, i.e. to study arbitrary sequences to identify properties and patterns.
Install Rust:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh- or update it with
rustup update
Build the software package:
cargo buildcargo test
The following types are available:
- Primes:
primes - Pythagorean primes:
pythagoreans - Sophie Germain primes:
sophie-germain - Twin primes:
twins - Happy numbers:
happy - Semiprimes:
semiprimes - Squarefree semiprimes:
squarefree-semiprimes - Pernicious numbers:
pernicious - Prime powers:
prime-powers - Fermi-Dirac primes:
fermi-dirac - Sphenic numbers:
sphenic-numbers
Types can be combined: + matches numbers satisfying any of the given types (union), X matches numbers satisfying all of them (intersection). For example primes+semiprimes or primesXpernicious. The two operators cannot be mixed in the same expression.
For graph output only, two types can be overlaid with distinct colors using ,: e.g. primes,semiprimes renders the first type in red, the second in blue, and overlap in purple.
All arguments are optional named flags; order doesn't matter.
-t, --type TYPE— number type or combination (default:primes)-s, --start START— starting number, inclusive (default:1)-e, --stop STOP— number of results forb-file; exclusive end bound forrange; canvas side forgraph(default:1000000/1024)-o, --output OUTPUT—b-file,rangeorgraph(default:b-file)
graph output requires stop to be a power of two up to 65536: it is the side of the square canvas. The default --stop 1024 gives a 1024×1024 canvas; --stop 4096 gives 4096×4096. --start is capped at 2^32, as is --stop outside graph mode.
Runs are additionally bounded by an internal sieve ceiling of 2^35: a request that would need a larger sieve — e.g. a b-file of more than roughly a billion terms, fewer for sparse types — fails with an error instead of exhausting memory.
Graph mode holds one byte per candidate, so peak memory grows with the square of the side: --stop 1024 ~1MB, --stop 4096 ~21MB, --stop 16384 ~350MB, --stop 65536 ~5.9GB. The largest sizes are allowed but need the memory to match.
range output lists every term in [start, stop) instead of the first stop terms — the form OEIS "number of X up to 10^n" sequences need. It sieves the range once, skipping the bound-estimation retry loop that count mode needs, so it is the cheaper path when the end is already known.
Graph mode writes three PNG files:
spiral.png— Ulam spiralline.png— sequential (row-major) layoutzorder.png— Z-order (Morton) curve
Examples:
cargo run -- -t semiprimes -s 1 -e 10— first 10 semiprimes as a b-filecargo run -- -t primes -o graph— three 1024×1024 graphs of primes up to 1048576cargo run -- -t "primes,semiprimes" -o graph— color overlay of primes (red) and semiprimes (blue)
