This project implements a Prime Detector → Prime Computer pipeline inspired by Willans’ Formula for computing the n-th prime number.
Willans’ original formula uses Wilson’s Theorem, which relies on factorials and is extremely slow.
This project replaces that component with a faster trial-division prime detector, allowing a simplified and educational reconstruction of the formula in code.
The final program contains:
- A Prime Detector
D(x) - A Prime Counting Function
π(m) - A modified Willans-style n-th prime summation
This satisfies the requirement of constructing a Prime Computer from a Prime Detector.
The logic is implemented inside:
class PrimeComputer:Returns:
1if x is prime0if composite
Uses deterministic trial division up to sqrt(x).
Complexity:
O(√x)
Computes:
[ \pi(m) = \sum_{j=1}^{m} D(j) ]
Counts how many primes ≤ m.
Implements the modified Willans summation:
[ p_n = 1 + \sum_{m=1}^{2^n} \left\lfloor \left( \frac{n}{1+\pi(m)} \right)^{1/n} \right\rfloor ]
Key idea:
- If π(m) < n → term = 1
- If π(m) ≥ n → term = 0 → summation stops early
This gives the n-th prime.
Save as:
prime_computer.py
Run:
python prime_computer.py
Example:
Enter the value of n: 10
Result: The 10-th prime number is 29
from prime_computer import PrimeComputer
pc = PrimeComputer()
print(pc.compute_nth_prime(20)) # Output: 71O(√x)
O(m√m)
Worst-case upper bound:
2^n
🚨 Exponential growth → extremely slow for large n.
This matches Wilf’s argument that some formulas are mathematically correct but computationally useless.