Project Goal & Tool Purpose
Prime Fold is a JavaScript web application for discovering mathematical functions that generate or embed prime numbers using evolutionary algorithms and advanced fitness functions. The goal is to help you explore, visualize, and evolve mathematical formulas that reveal interesting patterns in the distribution of prime numbers.
- Find functions that generate many unique primes (PrimeGen mode)
- Find pairs of functions that map primes to well-distributed 2D points (PrimeFold mode)
- Visualize and compare the structure of primes and composites
- Experiment with different optimization algorithms and fitness metrics
Modes
- PrimeFold Mode (2D Embedding): Enter or evolve two functions,
f_x(n)
and f_y(n)
, to map numbers to 2D points. Primes and composites are visualized differently. This mode helps you discover spatial patterns and structures unique to primes. Example: f_x(n) = n, f_y(n) = n^2
or simply n, n^2
.
- PrimeGen Mode (1D Generation): Enter or evolve a single function,
f(n)
, to generate numbers. The app visualizes which outputs are prime and how many unique primes are produced. Example: f(n) = 2*n + 1
.
Why Use Different Modes?
- PrimeFold is for exploring geometric and spatial properties of primes in 2D, revealing patterns not visible in 1D sequences.
- PrimeGen is for finding formulas that generate primes efficiently, useful for number theory and algorithmic exploration.
PrimeFold Mode (2D Embedding)
Finds functions f(n) and g(n) that map prime numbers to well-distributed 2D points with interesting patterns.
- Separation: Measures spatial distance between prime and non-prime distributions
- Contrast: Measures how much more organized primes are compared to non-primes
- Significance: Ensures patterns are statistically meaningful vs random data
- Specificity: Ensures patterns are unique to primes, not general number properties
- Area Coverage: Prevents line generation and encourages 2D area-covering patterns
- Symmetry: When enabled, ensures f(n) and g(n) are symmetrical transformations (e.g., sin/cos pairs)
PrimeGen Mode (1D Generation)
Finds function f(n) that generates many unique prime numbers.
- Hit Ratio: Ratio of unique primes generated to total range
- Unique Numbers: How many different numbers are generated
- Unique Primes: How many of those are prime
Algorithms
- LAHC: Late Acceptance Hill Climbing - fast and effective
- GA: Genetic Algorithm - good for complex functions
- SA: Simulated Annealing - good exploration
Parameters
- Sample Size: Number of primes/composites to evaluate
- Display Points: Number of points to show in visualization
- Iterations: Number of optimization iterations
Writing Expressions
You can enter mathematical expressions to define functions for visualizing or generating primes. Expressions use the variable n
and support a variety of operators, functions, and constants.
- PrimeFold mode: Enter two expressions, separated by a comma, e.g.
f_x(n) = n, f_y(n) = n^2
or simply n, n^2
.
- PrimeGen mode: Enter a single expression, e.g.
f(n) = 2*n + 1
or just 2*n + 1
.
Operators
Operator | Description | Example |
+ | Addition | n + 2 |
- | Subtraction | n - 1 |
* | Multiplication | 2 * n |
/ | Division | n / 3 |
% | Modulo (remainder) | n % 2 |
mod | Modulo (alternative) | n mod 2 |
^ | Exponentiation | n^2 |
Functions
Function | Description | Example |
sin(x) | Sine (radians) | sin(n) |
cos(x) | Cosine (radians) | cos(n) |
sind(x) | Sine (degrees) | sind(n) |
cosd(x) | Cosine (degrees) | cosd(n) |
sqrt(x) | Square root | sqrt(n) |
log(x) | Natural logarithm | log(n) |
abs(x) | Absolute value | abs(n-10) |
floor(x) | Floor (round down) | floor(n/2) |
ceil(x) | Ceiling (round up) | ceil(n/2) |
square(x) | Square (x^2) | square(n) |
cube(x) | Cube (x^3) | cube(n) |
Constants
Name | Value |
pi | 3.141592... |
e | 2.718281... |
sqrt2 | 1.414213... |
phi | 1.618033... (golden ratio) |
Syntax Tips
- Use parentheses
()
to group expressions: (n + 1) * 2
- Both
^
and square(x)
, cube(x)
are supported for powers.
mod
and %
are equivalent for modulo.
- All functions take a single argument:
sin(n)
, sqrt(n+1)
, etc.
- You can omit
f(n) =
or f_x(n) =
/f_y(n) =
and just write the expressions directly.
- Spaces are ignored.
Examples
PrimeFold (2D):
f_x(n) = n, f_y(n) = n^2
n, n^2
sin(n), cos(n)
n, n mod 5
sqrt(n), log(n)
PrimeGen (1D):
f(n) = 2*n + 1
n^2 + 1
abs(n-10)
cube(n) - 1
If you enter an invalid expression, check the browser console for errors. For more details, see the README or the Expressions Guide in the repository.