An Exact Formula for the Primes: Willans' Formula
Why Mathematics Stack Exchange recommends it
Mathematics Stack Exchange users link it whenever someone asks about a formula for the nth prime or Willans' formula, as a clear walkthrough of how the formula works and why it is clever but impractical.
Recommended by 3 people across 3 different months, 2010 to 2024.
What it is
Eric Rowland's animated explainer (made with Manim) walks through Willans' 1964 formula for the nth prime, building it from a Wilson's-theorem prime detector, a prime counter, and a final summation step. It closes with a discussion of what counts as a formula, why this one is not useful in practice, and who Willans was.
What people said
Recommends Daniel P · 2023-04 · Mathematics Stack Exchange · on “Are there alternatives for the prime indicator function $ \mathbb{1}_{n \in \mathbb{P}}=-\mu(n) \frac{\Lambda(n)}{\log n} /(\cos \pi n)^2 $?”
I suggest this great video by Eric Rowland [this video] about "an exact formula for the n'th prime". Though it takes hyperexponential time to actually evaluate it for any $n \ge 2$, so you'd be better off with the Sieve of Eratosthenes, and it really only shows that algebraic tools can be used as an (inefficient) programming language. There are multiple inefficient formulas you can come up with, the one shown in the video is just one example.
Recommends ShreevatsaR · 2023-09 · Mathematics Stack Exchange · on “Is there a known mathematical equation to find the nth prime?”
@TheEmptyStringPhotographer As you say in your later edit to your answer, that formula (by C. P. Willans) is not useful for constructing a Taylor series or for any purpose really. I just linked from the answer a nice video [this video] about that formula; there are also good discussions about how such formulas are (clever, but) useless (“the disease was preferable to the cure”) in Herbert Wilf's “What is an Answer?” [LINK] and Underwood Dudley's “Formulas for Primes” [LINK] .
Recommends The_Animator · 2023-11 · Mathematics Stack Exchange · on “What do we call numbers that are neither prime nor composite?”
@AlgTop1854 Hope this helps– in case you didn't get it there's a video on it here (with the timestamp linked): [this video]
Cites ShreevatsaR · 2010-07 · Mathematics Stack Exchange · on “Is there a known mathematical equation to find the nth prime?”
No, there is no known formula that gives the nth prime, except artificial ones [this video] you can write that are basically equivalent to "the $n$ th prime". But if you only want an approximation, the $n$ th prime is roughly around $n \ln n$ (or more precisely, near the number $m$ such that $m/\ln m = n$ ) by the prime number theorem [LINK] . In fact, we have the following asymptotic bound on the $n$ th prime $p_n$ : $n \ln n + n(\ln\ln n - 1) < p_n < n \ln n + n \ln \ln n$ for $n\ge{}6$ You can sieve [LINK] within this range if you want the $n$ th prime. [Edit: Using more accurate estimates you'll have a much smaller range to sieve; see the answer by Charles.] Entirely unrelated: if you want to
Cites Benjamin Trumpy · 2022-11 · Mathematics Stack Exchange · on “Is this function good at finding the nth prime? What is it's big O?”
$$f\left(m\right)=\sum_{i=0}^{m^{2}}⌊cos\left(⌊ \frac{\sum_{o=1}^{i}\left(\prod_{n=2}^{⌊\sqrt{o}⌋}\left(1-⌊\cos\left(\pi\frac{o}{n}\right)^{2}⌋\right)\right)}{m+1}⌋\right)^{2}⌋$$ I was watching a video about how a formula for finding the nth prime, Willan's formula, and how it did that. What this function does is similar, and can be broken down into a few different parts. Part 1 $$g\left(o\right)=\prod_{n=2}^{⌊\sqrt{o}⌋}\left(1-⌊\cos\left(\pi\frac{o}{n}\right)^{2}⌋\right)$$ This section of the function is a primality detector. If a number is prime, It outputs a $1$ , but if it is composite, It outputs a $0$ . Part 2 $$h\left(i\right)=\sum_{o=1}^{i}\left(\prod_{n=2}^{⌊\sqrt{o}⌋}\left(1-⌊\cos\
Cites Ataulfo · 2022-12 · Mathematics Stack Exchange · on “The most accurate estimate for the nth prime”
COMMENT.-An spectacular formula giving exactly the $n^{th}$ prime was appeared in $1964$ . In the link below figure C.P. Williams as the author. I remember I read this in a small peruvian magazine and I believed the author was a peruvian really. It is the reason I am not sure that Williams was the author. $$n^{th}\space \text {prime }=1+\sum_{i=1}^{2^n}\left\lfloor\left(\frac nD\right)^{\frac 1n}\right\rfloor$$ where $$D=\sum_{j=1}^i\left\lfloor\cos\left(\frac{(j-1)!+1)\pi}{j}\right)^2\right\rfloor$$ . [this video] In those times (the $60$ 's) there were no computers for public use, so the formula could not be verified. But currently it has been possible to verify the accuracy up to certain limit
Cites g.kertesz · 2023-01 · Mathematics Stack Exchange · on “Have you ever been very surprised that something has, or doesn't have, a closed form?”
As others explained, it is very much up to how we define 'closed formula'. To illustrate how you can cleverly cheat with the floor function and such, I present you Willan's formula (1964) to generate the n-th prime: $$p_n=1+\sum_{i=1}^{2^n} \left\lfloor \left(\frac{n}{\sum_{j=1}^{i}\left\lfloor\left(\cos\frac{(j-i)!+1}{j}\pi\right)^2 \right\rfloor} \right)^{1/n}\right\rfloor $$ See [this video] for details.
Cites Jaap Scherphuis · 2023-02 · Mathematics Stack Exchange · on “I wrote a formula for prime numbers. Is it good enough to write article about it or not worth it?”
As for the rest of your work, assuming it is correct, it seems to me a bit like Willans' formula [this video] . It shows off a clever construction method, shows how a particular set of simple tools is powerful enough to construct a particular complicated thing, but it is not useful in practice.
Cites Tsar Asterov XVII · 2023-03 · Mathematics Stack Exchange · on “Why is Willans formula for prime's not considered a valid formula?”
So, I was watching this video on Willan's formula by Eric Rowland [this video] , and it was mentioned that this formula does not count for generating primes, as it is extremely hard and time consuming to compute. I was wondering why it would not be considered as valid? While it is time consuming to compute, I don't feel it's valid to not consider it an equation, as pure mathematics is often not restrained by our physical limitations. Just because a formula is hard for us to calculate does not really make it invalid I feel, as in a hypothetical world such as math, physical boundaries such as computation time does not make sense. While it is not the most efficient way to calculate, I still feel it
Cites Yusuf Maher · 2024-04 · Mathematics Stack Exchange · on “Challenge: Define rounding functions using integer checking functions”
This question was inspired by Willans' formula, in which he used the cosine function as a way to constrain a number and floor it to be able to count primes (see [this video] ): $$p_n = 1 + \sum_{i=1}^{2^n} ⌊(\frac{n}{\sum_{j=1}^i⌊\cos^2(\pi{\frac{(j-1)!+1}{j}})⌋})^{1/n}⌋$$ My goal was to create an integer checking function using no rounding functions or absolute value function. I have already come up with $f(x) = \lim \limits_{n \to \infty} \cos^2(\pi x)^n$ , which uses a limit to essentially floor a number between 0 and 1. Note that the function returns 0 for a non-integer, and 1 for an integer. What other integer checking functions can be made? Is it possible to define the floor, ceiling, or ev