The video provides a sharp reality check by exposing how a mathematically "elegant" formula can be nothing more than a computationally expensive tautology. It serves as a perfect lesson that in number theory, existence does not imply efficiency.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
This Formula Outputs N-th Primes. It's UselessAdded:
Why is pi here? Why is there a trigonometric operation, a cosine squared, the thing you use to describe a rotating radius inside a formula whose only job is to count prime numbers? Pi belongs to circles, cosine belongs to triangles and waves, to the shadow a rotating arrow casts on a wall. And primes, primes belong to divisibility. A prime's a number that can't be split into smaller factors. That's a discrete arithmetic integer world property. It has nothing to do with circles. These worlds shouldn't touch. And yet there's a formula written in 1964 by a schoolmaster in England that stitches them together. You give it a positive integer n and it hands back the nth prime, the second prime, the 10th, the millionth, any of them. And sitting in the middle of that formula is a cosine multiplied by pi. Something's off.
Either pi's secretly doing something arithmetic we haven't noticed, or the formula's pulling a trick, smuggling a hard problem past you while you're distracted by the wallpaper. I'll tell you right now, it's the second one. But figuring out how and why turns out to be one of the stranger lessons in mathematics about what the word formula even means. There's a payoff at the end, a question you probably haven't heard asked out loud. Stay with me. First, why this is a question worth asking at all.
Primes resist patterns. That's essentially their defining feature.
They're the atoms of the integers. Every whole number above one is a product of primes in exactly one way. And yet the sequence itself, 2 3 5 7 11 13 17, has no simple rule. No arithmetic progression catches them. No low degree polynomial spits them out. Nobody's ever found a clean shortcut. What we do have are facts around the edges. Euclid proved three centuries before the common era there are infinitely many primes.
The prime number theorem settled at the end of the 19th century tells us the nth prime grows roughly like n times the natural log of n. An asymptotic statement, a description of the shape at large scales, not a rule for picking out any particular term. There's Mills constant from 1947, a real number approximately 1.3063 with the property that the floor of it raised to 3 to the n is always prime.
Lovely. Also non-constructive. The constant's defined by demanding the formula work. You can't compute it unless you already know the primes.
There's the Matiyasevich polynomial from 1971. 26 variables, degree 25, whose positive outputs as the variables range over non-negative integers are exactly the primes. Also a restatement of primality, rather than a shortcut to it.
So when someone says there's a closed form formula for the nth prime, a real one, elementary one line long with a cosine squared and a factorial and some floors, you do a double take and you ask, "Where did the pi come from?" The answer starts with a theorem from 1770.
The entire construction rides on one number theoretic fact. Wilson's theorem, stated by Edward Waring in 1770, attributed to his student John Wilson, and first proven by Lagrange shortly after. The statement's short. Take a whole number p greater than or equal to two. Compute the factorial of p minus one, the product of every positive integer below p. Add one. Is the result divisible by p? Wilson's answer, it's divisible by p if and only if p is prime. Primes make the factorial plus one divisible, composites don't. Small cases. For j equal to two, the factorial of one is one, plus one is two, divided by two is one, clean. For j equal to three, the factorial of two is two, plus one is three, divided by three is one, clean again. For j equal to four, the factorial of three is six, plus one is seven. Seven over four is 1.75, not clean. Four isn't prime. For j equal to five, the factorial of four is 24, plus one is 25, divided by five is exactly five, clean. Five is prime. The factorial explodes. The divisibility snaps into place only at the primes.
Wilson's theorem has turned primality, which plainly asks whether any integer below p divides p, into a single yes or no arithmetic test. But a yes or no test isn't a number. A formula needs numbers.
We need to take this divisibility check and make it spit out a one when something's prime, a zero when it isn't.
And we need to do it without branching, without an if statement. Formulas don't have if statements. They only have arithmetic. How do you turn is this a whole number into a value of one or zero using nothing but arithmetic? That's the question that forces the cosine into the building. Here is the trick. We want a function of j, call it f of j, that evaluates to exactly one when j is prime and exactly zero when j is composite. An indicator, something we can sum. The bridge is the cosine. The factorial of j minus one plus one divided by j, call that the ratio. Wilson tells us the ratio's a whole integer precisely when j is prime. For composite j, it's a fraction, some integer plus a leftover strictly between zero and one. Feed the ratio into cosine multiplied by pi.
Cosine of pi times a whole integer is plus or minus one. It lands on the horizontal axis of the unit circle, exactly. Cosine of pi times any non-integer has absolute value strictly less than one. It doesn't reach the axis. That's what pi is doing. It isn't measuring a circle. It's a ruler, a spacing that makes integers line up on the peaks and valleys of cosine, and non-integers not. Pi distinguishes whole number from not a whole number in the language of a continuous function.
Squaring handles the sign. Cosine squared of pi times an integer is one.
Cosine squared of pi times a non-integer sits in the half-open interval from zero up to but not including one. Apply the floor. Floor of one is one. Floor of anything strictly below one is zero.
That's the indicator. A cosine squared gadget sandwiched between pi times the Wilson ratio on the inside and a floor on the outside, one at primes, zero at composites. Almost. There's a quirk worth flagging because it'll matter. At j equals one, the construction returns one. It treats the number one as if it were prime. The factorial of zero is one, plus one is two, divided by one is two. Cosine squared of 2 pi is one.
Floor is one. f of one equals one. One isn't prime. So the indicator's slightly wrong at j equals one and nowhere else.
You'd think that's a bug. It's not. The formula doesn't just tolerate the error, it weaponizes it. The off by one at j equals one is exactly the off by one the next stage needs to avoid a division by zero. You'll see it in a moment. Let pi of i, of lowercase pi, not the circle constant, be the prime counting function, the number of primes less than or equal to i. Pi of one is zero. Pi of two is one. Pi of three is two. Pi of 10 is four. The primes 2 3 5 7. A staircase climbing by one at every prime, flat in between. Sum our indicator f of j from j equals one up to j equals i, and you're almost computing pi of i. Almost because of the spurious one at j equals one.
Every f after that behaves correctly. So the running sum gives pi of i plus one.
Call that shifted staircase s of i. s of i equals one plus pi of i. Here's why the quirk was useful. s of i is never zero, even at i equals one, where no primes exist yet. s of i is one, not zero, because in the next stage we're going to divide by this quantity. If the staircase started at zero, we'd crash on the very first term. The spurious one lifts the floor of the staircase by exactly the amount we need to keep the division safe. A bug in a feature, same mechanism. We've now got a closed form counter for the primes, built out of factorials and pi and a cosine and a floor. But we don't want a counter, we want the inverse. Given n, we want the location where the staircase takes its nth step, because that location is the nth prime. How do you invert a staircase using nothing but arithmetic? Not a search, not an if, just operations.
That's the next layer. Here's the cleverest bit of the whole construction.
s of i starts at one and climbs step by step past each prime. The nth prime is the point where s of i first reaches n plus one, because the staircase had to take n upward steps to get there. We want to convert first reaches n plus one into a sum of indicators, something that fires one at every i strictly below the nth prime, and zero from the nth prime onward. Indicators all the way down.
Consider this. Take n divided by s of i, raise the result to the one over n power. Apply floor. Watch what happens.
As long as s of i is less than or equal to n before we hit the nth prime, the ratio n over s of i is at least one.
Raised to the one over n power, it stays at least one and strictly less than two.
Floor gives exactly one. The moment s of i exceeds n, the staircase has just climbed past the nth prime, the ratio drops below one. Raised to any positive power, it stays below one. Floor gives zero. So this outer nested floor is itself an indicator, one for every i strictly below the nth prime, zero from the nth prime onward. It flips at exactly the right place. Sum that outer indicator from i equals one up to some large upper bound. You get the count of positions where it fires one, the count of integers strictly below the nth prime. That count is the nth prime minus one. Add one. Done. You've got the nth prime. The whole formula in full. One plus a big outer sum of a nested floor, wrapping a ratio, wrapping a sum of cosine squared indicators. Four layers built from the inside out. Each layer undoes the one below it. The upper bound is two to the n. The reason's Bertrand's postulate, proven by Chebyshev in 1852.
For every integer m greater than or equal to one, there's at least one prime between m and 2m. Between any number and its double, a prime. Iterate. The first prime is two. The second's at most twice the first, at most four. The third, at most twice the second, at most eight. In general, the nth prime's at most two to the n. If the outer sum runs up to two to the n, it's guaranteed to have marched past the nth prime. The indicator's already flipped to zero beyond. Zeros don't contribute. One footnote a careful viewer will spot. The bound's sometimes stated strictly less than two to the n. That's not quite right at n equals one, where the first prime's two, which equals two to the first. The formula uses an inclusive upper bound and the equality case works out. So, the outer sum runs from 1 up to 2 to the n and it works. That word works is doing a lot of hiding because running up to 2 to the n isn't a bookkeeping detail. 2 to the n grows exponentially and we're about to evaluate an indicator that internally computes a factorial at every turn. You might already be feeling the shape of what's going to go wrong.
Let's watch the machine actually run. At n equals 4, the fourth prime is 7. The outer sum runs from i equals 1 to i equals 16. That's 2 to the fourth. For each i, we compute s of i. It begins at 1, climbs to 2 after the first prime, to 3 after the second, and so on. Across i from 1 through 16, the staircase reads 1 2 3 3 4 4 5 5 5 5 6 6 7 7 7 7. The flat stretches are composite runs. The step-ups are the primes. Now, the outer floor. The ratio of 4 divided by s of i raised to the 1/4 power floored. As long as s of i is less than or equal to 4, the staircase hasn't climbed past 4. The floor returns 1. That happens for i from 1 through 6. After that, s of i is 5 or more, the ratio drops below 1, and the floor returns 0. So, the outer sum's 1 + 1 + 1 + 1 + 1 followed by 10 zeros.
Total 6. Add the final + 1 on the outside. 7. The fourth prime is 7. The formula agrees. Run the same machine at n equals 1 2 3 5 6. You get 2 3 5 11 13 every time. It's correct. It's mechanical. It's in a real and rigorous sense a closed-form expression for the nth prime, which is precisely what makes the next part so uncomfortable. Because the formula works and it's beautiful and it's wrong. Not wrong in its answer, but wrong in what it's pretending to have done. Now, the question you should have been sharpening the whole time. Is this a way to compute primes? Take n equals 10. The 10th prime is 29. The formula dutifully sums i from 1 up to 1,024.
Along the way, it needs the cosine squared indicator, which means it needs the factorial of 1,023.
That number has roughly 2,637 decimal digits. At every step. By contrast, a sieve of Eratosthenes, the algorithm a Greek mathematician published 2,200 years ago, finds the 10th prime in a handful of operations on small integers. A laptop does it in microseconds. Now, take n equals 100.
The 100th prime is 541. A sieve needs a few hundred operations on small numbers.
The Willans formula needs to sum i from 1 up to 2 to the 100th power, a number with more than 30 decimal digits. The largest factorial it evaluates at the top of the sum is the factorial of 2 to the 100 minus 1. By Stirling's approximation, that factorial has roughly 3.76 * 10 to the 31st decimal digits. Not 3.76 * 10 to the 31st. The number of digits in that quantity is already that large. Writing out just the digit count would take more atoms than the Earth contains. Even at 10 billion big integer multiplications per second, evaluating the sum would take on the order of 10 to the 20th seconds. The age of the universe is about 1.4 * 10 to the 10th years. The formula would need roughly 3 trillion times that to compute the 100th prime. A sieve does it in a blink. So, the formula is correct in exactly the way that the third tallest mountain in the solar system is correct.
It picks out the right object. It doesn't help you find it. And here finally is the answer to the question you've been carrying since the first section. Why is pi there? It's bookkeeping. The formula works because it's encoded the definition of primality inside the factorial. Wilson's theorem is a compressed way of asking, is this number prime? The exact same question you started with. The cosine, the floors, the power trick, they aren't doing arithmetic work. They're translation. They convert a yes or no test, the same one you do by hand, into a one or a zero, so the formula can add those ones and zeros up inside a sum sign. Pi isn't doing number theory. It's being used as a dictionary. And beneath every term of that sum, the formula is checking primality by computing a factorial and looking at a remainder.
Mills' constant does the same thing. The Matiyasevich polynomial does the same thing. Different costumes, same trick.
None of them compute primes faster than the sieve. The cosine was the costume.
Pi was the lighting. The primality test was always still there doing the work.
So, what did we learn? A formula, loosely, is a closed-form expression. A finite arrangement of symbols that evaluates to an answer. A useful formula is something more. It trades search for computation. The quadratic formula takes three coefficients and returns the roots with four arithmetic operations and square root. It replaces a search, trying candidate roots, with a direct calculation. The search is gone.
Willans' formula is closed-form, but it doesn't trade anything. The search is still there, hidden inside the factorial, folded up, tucked behind a cosine, but it hasn't disappeared. You couldn't compute a millionth prime this way even with the entire mass energy of the observable universe. It's a mathematical Russian doll. The nth prime is inside, all right, but every time you open one shell, the next shell down is exactly the primality test you were trying to avoid. Unwrap the cosine, you find a factorial. Unwrap the factorial, you find Wilson's theorem. Unwrap Wilson's theorem, you find the definition of prime looking back at you.
So, it exists. And now you know why mathematicians shrug when someone waves it at them. The closed form's real. The computation isn't. Pi was never the mystery. The factorial was. Here's the question worth sitting with. What would a genuinely useful formula for the nth prime even look like? Something that lets you jump to the millionth prime without walking past every number before it. Something that skips the search entirely. Nobody's found one. Most number theorists suspect there isn't one. That the primes are, in some deep sense, computationally incompressible.
That the sieve is, up to constants, the best you can ever do. If that's true, then Willans' formula isn't a failure.
It's a monument. A closed-form expression for an object that refuses to be compressed, built from the one piece of structure, Wilson's theorem, that comes close enough to pretend. And the cosine with the pi beside it, sitting at the center of a formula for the primes.
It was never the shortcut. It was the disguise.
Related Videos
Olympiad Mathematics | Indian | Can You Solve This One?
PhilCoolMath
650 views•2026-06-03
Escaping the Fog
LogicLemurGaming
760 views•2026-06-03
A Brutal Radical Expression Made Easy! The Shortcut Changes Everything.
tamoshop
112 views•2026-06-02
V : jee main /advance class 11 mathematics : Binomial Theorem class-1 ( 29 may 2026 )
dcamclassesiitjeemainsadva9953
125 views•2026-05-29
Is This Pentomino Tileable?
3cycle
241 views•2026-05-30
This Sudoku Has Many Lines!!
CrackingTheCryptic
2K views•2026-05-29
Olympiad Mathematics | Indian Can You Solve This One?
PhilCoolMath
268 views•2026-06-02
Olympiad Mathematics | Indian | Can You Solve This?
PhilCoolMath
669 views•2026-06-02











