(ii) Compute the time complexity of Willans’ formula and compare it with the time complexity of your formula
- Let
$p_n$ denote the$n$ -th prime. By the Prime Number Theorem (using safe Rosser bounds):
$$p_n = \Theta(n \log n).$$ - Although Willans-style formulas use a formal upper limit
$N$ (e.g.,$N = 2^n$ ), the summand becomes zero for$m \ge p_n$ .
Thus the effective limit is$m \approx p_n$ .
- Willans uses Wilson’s theorem:
$j$ is prime iff$(j-1)! + 1$ is divisible by$j$ . - Computing
$(j-1)! \bmod j$ requires$\Theta(j)$ multiplications per$j$ on very large integers.
This is exponential.
Using
Your detector checks divisors up to
Substitute
-
Practical asymptotics:
- Willans:
$(n \log n)^3$ - Your method:
$(n \log n)^{5/2}$
→ Faster by about$(n \log n)^{1/2}$ .
- Willans:
-
Willans is much slower in practice because:
- factorial values are enormous,
- arithmetic operations are very expensive,
- constants are huge.
| Method | Effective limit | Practical time (in |
Time in |
|---|---|---|---|
| Willans (factorial) | |||
| Your method (trial division) | |||
| Sieve of Eratosthenes | primes |