My original C and early JavaScript experiments with prime numbers, natural decomposition, and the patterns between consecutive primes.
I worked on these experiments in college, before AI coding tools were part of the process. I'm proud of this work, especially the attempt to divide a sieve's work across threads. This repository keeps the original source available to explore, including the unfinished parts.
For the later interactive browser project, see fun_with_primes. It carries forward these C sources alongside a separate JavaScript Web Worker implementation.
| If you're curious about… | Start here |
|---|---|
| The threaded sieve | eratosthenes.c and the explanation below |
| Generating and printing a prime list | primeList.c |
| Natural-number decomposition | naturalDecomposition.c and eratosthenesFull |
| Gaps and patterns between primes | primeDifference.c and primeSieveDifference.c |
| Prime frequency over intervals | primeFrequency.c |
| Early browser experiments | javascript/index.html, with primality, factor, and sieve functions |
threadPartialSieve is the worker routine for pth_eratosthenesPrime. The source header credits Vincent T. Mossman and records an update on November 5, 2016; the corresponding source history preserves that stage of the experiment.
The design divides the sieve's factor-marking passes among POSIX threads:
- The caller initializes a shared
intflag array with one entry per number from 1 throughn. - It starts the number of workers specified by the mutable global
numberOfThreads, initialized to4in this source. There is no automatic CPU-count detection. - Each worker starts at index
i = rank + 1, advances bynumberOfThreadswhilei < sqrt(n), and marks larger multiples ofi + 1as composite. The code skips later passes for even factors. - After joining the threads, the caller counts the remaining flags and copies their numbers into an
unsigned longresult array.
All workers operate on the same flag array; they do not own separate number ranges. This reduced sieve is intended to produce a prime list, while eratosthenesFull supports the separate natural-decomposition experiments.
These are individual experiments rather than a single packaged application. Several source headers contain their original build and usage notes. For example, primeList.c documents:
gcc primeList.c -pthread -lm -o primeList
./primeList 100That command requires a C compiler, POSIX threads, and the math library. Treat it as historical guidance rather than a guarantee for every current toolchain. Review the source before running it, start with a small input, and build locally rather than relying on the committed binaries.
The browser files live in javascript/. The HTML page loads the accompanying scripts directly and includes an external Google Fonts stylesheet.
- The threaded sieve leaves the flag for
1set, so its output includes1even though it is not prime. - Threads can write the same flag-array entries without synchronization. Correctness and thread safety need review before reuse.
- Some experiments are unfinished and may need fixes to compile with a current toolchain.
naturalDecomposition.candprimeFrequency.copen their corresponding.txtoutput files for writing, replacing existing contents.- Timing output is part of the experiments; this repository makes no benchmark or performance guarantee.
The original source, comments, and filenames are preserved here so the work can be read in its own context.
