Open-access mathematical research insights
About Contact
Home / Free Stuff

Buckets (C++)

A simple allocation of primes along the number line

Prime Heat Map

Buckets is a short piece of code designed to generate a heat map of primes across the number line. The screen is segmented into bins of 10x10 pixels and primes are allocated according to their position.

This iteration could be improved with a more sophisticated allocation of the primes. In fact this is most likely as unsophisticated as one could be but it is a good starting point for understanding prime distribution visually.

Prime heat map visualization showing concentration around smaller integers
Prime frequency concentrated around smaller integers

C++ Implementation

This code uses SDL2 for graphics rendering. You'll need SDL2 installed plus helper headers for prime generation.

C++ (SDL2)
#include <SDL2/SDL.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include "primes.h"
#include "drawing.h"

// Function to count primes in each bin
std::vector<std::vector<int>> countPrimesInBins(const std::vector<int>& primes, int width, int height, int binSize) {
    std::vector<std::vector<int>> bins(width / binSize, std::vector<int>(height / binSize, 0));
    for (int prime : primes) {
        int x = prime % width;
        int y = prime / width;
        bins[x / binSize][y / binSize]++;
    }
    return bins;
}

// Function to map prime count to a color
SDL_Color getBinColor(int primeCount, int maxCount) {
    int intensity = static_cast<int>(255.0 * primeCount / maxCount);
    return SDL_Color{255, static_cast<Uint8>(intensity), static_cast<Uint8>(intensity), 255};
}

int main() {
    const int WINDOW_WIDTH = 800;
    const int WINDOW_HEIGHT = 600;
    const int BIN_SIZE = 10;
    int numberOfPrimes;

    std::cout << "Enter the number of prime numbers to generate: ";
    std::cin >> numberOfPrimes;

    std::vector<int> primes = generatePrimes(numberOfPrimes);
    std::vector<std::vector<int>> bins = countPrimesInBins(primes, WINDOW_WIDTH, WINDOW_HEIGHT, BIN_SIZE);

    int maxCount = 0;
    for (const auto& row : bins) {
        maxCount = std::max(maxCount, *std::max_element(row.begin(), row.end()));
    }

    if (SDL_Init(SDL_INIT_VIDEO) != 0) {
        std::cerr << "SDL_Init Error: " << SDL_GetError() << std::endl;
        return 1;
    }

    SDL_Window* window = SDL_CreateWindow("Prime Heatmap", SDL_WINDOWPOS_CENTERED,
        SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, 0);
    if (window == NULL) {
        std::cerr << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
        SDL_Quit();
        return 1;
    }

    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    if (renderer == NULL) {
        std::cerr << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
        SDL_DestroyWindow(window);
        SDL_Quit();
        return 1;
    }

    bool running = true;
    SDL_Event event;
    while (running) {
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                running = false;
            }
        }

        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
        SDL_RenderClear(renderer);

        for (int x = 0; x < WINDOW_WIDTH; x += BIN_SIZE) {
            for (int y = 0; y < WINDOW_HEIGHT; y += BIN_SIZE) {
                int binCount = bins[x / BIN_SIZE][y / BIN_SIZE];
                SDL_Color color = getBinColor(binCount, maxCount);
                SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
                SDL_Rect binRect = {x, y, BIN_SIZE, BIN_SIZE};
                SDL_RenderFillRect(renderer, &binRect);
            }
        }

        SDL_RenderPresent(renderer);
    }

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}

Compiling

Terminal
# Install SDL2 (macOS)
brew install sdl2

# Compile
g++ -std=c++17 buckets.cpp -o buckets \
    -I/usr/local/include -L/usr/local/lib \
    -lSDL2

# Run
./buckets

Stay Updated

Get weekly digests of new research insights delivered to your inbox.