Skip to content

the-ethan-wang/prime-spiral-generator

Repository files navigation

Prime spiral generator

Inspired by 3b1b's video on prime spirals and more.
Generates unique prime spirals with Python and matplotlib.
The script plots prime numbers with a random colour map onto a grid using polar coordinates.
It used to use Sieve of Eratosthenes which is like O(N log (log N)) but now it uses a caching system you can check that out in gen_scripts/prime_gen and choose which one to use in gen_scripts/ps_plt_gen.py.
Actually the sieve is faster im not sure why im gonna optimise the cache method.


Sample images

Sample outputs (from sample_images/):

Sample 1 Sample 2 Sample 3
Random spiral #2 Random spiral #9 Random spiral #12
Sample 4 Sample 5 Sample 6
Random spiral #15 Random spiral #17 Random spiral #19

sample_images/ includes 20 sample images

Sample grid outputs (from grid_images/):

Grid 1 4 by 4 grid

Grid 2 33 by 33 grid

sample_images/ includes 2 sample grids


Installation

Requirements

  • Python 3.10+

    matplotlib
    random
    math
    

Setup

  1. Clone this repository:

    git clone https://github.com/ethanwang314159/prime-spiral-generator.git
    cd prime-spiral-generator
  2. Install dependencies:

    pip install -r requirements.txt

Usage

python main.py

Generates 16 unique spirals and then a 4 by 4 grid with those spirals into rand_images and grid_images.


File Structure

prime-spiral-generator/
│
├── gen_scripts/
│   ├── next_fp_gen.py          # Finds next available file path in folder
│   ├── ps_plt_gen.py           # Generates prime spiral plot (returns plt)
│   ├── rand_settings_gen.py    # Creates random settings
│   ├── grid_gen.py             # Generates a grid of spirals
│   ├── empty_folder_gen.py     # 
│   └── prime_gen
│       ├── get_primes_cache.py # Getting primes with cache
│       ├── get_primes_sieve.py # Getting primes with sieve
│       ├── primes_cacher.py    # Cacheing primes 
│       ├── primes_cache.rs     # Cached primes storage
│
├── rand_images/                # Empty (for now)
│
├── temp_images/                # Temp images for large grid generation(not saving each spiral)
│
├── sample_images/
│   └── 0.png ... 19.png        # Sample images
│
├── grid_images/
│   └── grid0.png               # 4x4 Sample grid image
│   └── grid1.png               # 33x33 Sample grid image
│
└── main.py                     # Main script for spiral generation

Todo

  • Animation: visualise the spirals forming like the primes getting loaded in (maybe gifs).
  • Interactive: adjustable spiral settings in a good ui. like change the size by dragging/clicking not typing a number.
  • Vote: a website where you vote between two spirals and using an algorithm like the elo system find the perfect spiral with a leaderboard etc
  • Optimise: make it faster
  • Tweak the settings: make it better