# Formula for primes

Willan's Formula is a formula that can find the nth prime number.

${\displaystyle 1+\sum _{i=1}^{2^{n}}{\lfloor ({\frac {n}{\sum _{j=1}^{i}{\lfloor (\cos {\pi {\frac {{(j-1)!}+j}{j}}})^{2}\rfloor }}})^{\frac {1}{n}}\rfloor }}$

## Proof

Let's first start with the ${\displaystyle {\frac {(j-1)!+1}{j}}}$.

Wilson's theorem says if ${\displaystyle {(j-1)!+1}}$ is divisible by ${\displaystyle j}$, than ${\displaystyle j}$ is either a prime number or ${\displaystyle 1}$, meaning when ${\displaystyle j}$ is prime, ${\displaystyle {\frac {(j-1)!+1}{j}}}$ is an integer.

It would be much easier if the formula gives a number instead of checking if the number is an integer, and we can do this with the ${\displaystyle {\lfloor (\cos {\pi {\frac {{(j-1)!}+j}{j}}})^{2}\rfloor }}$ part.

The reason the formula has ${\displaystyle \pi }$ multiplied by the ${\displaystyle {\frac {(j-1)!+1}{j}}}$ part is because when ${\displaystyle {\frac {(j-1)!+1}{j}}}$ is an integer, ${\displaystyle \cos({\frac {(j-1)!+1}{j}}\pi )}$ will give ${\displaystyle 1}$ or ${\displaystyle -1}$.

When squaring the result then ${\displaystyle \cos({\frac {(j-1)!+1}{j}}\pi )}$ will equal ${\displaystyle 1}$ when ${\displaystyle {\frac {(j-1)!+1}{j}}}$ is an integer.

By flooring this, the only results are ${\displaystyle 1}$ when ${\displaystyle {\frac {(j-1)!+1}{j}}}$ is an integer and ${\displaystyle 0}$ when it isn't, leaving${\displaystyle {\lfloor (\cos {\pi {\frac {{(j-1)!}+j}{j}}})^{2}\rfloor }}$.

The ${\displaystyle \sum _{j=1}^{i}{\lfloor (\cos {\pi {\frac {{(j-1)!}+j}{j}}})^{2}\rfloor }}$ will add ${\displaystyle 1}$s for the primes ${\displaystyle 1}$ - ${\displaystyle i}$ and and will sum up to the ${\displaystyle (\mathbin {\#} {\textrm {primes}}\leq {i})+1}$.

The ${\displaystyle {\lfloor ({\frac {n}{\sum _{j=1}^{i}{\lfloor (\cos {\pi {\frac {{(j-1)!}+j}{j}}})^{2}\rfloor }}})^{\frac {1}{n}}\rfloor }}$ in short will give ${\displaystyle 1}$ if ${\displaystyle n>(\mathbin {\#} {\textrm {primes}}\leq {i})}$ and ${\displaystyle 0}$ when ${\displaystyle n\geq (\mathbin {\#} {\textrm {primes}}\leq {i})}$.

Take the ${\displaystyle p(x)}$ of both sides where ${\displaystyle p(x)}$ is the nth prime number:

${\displaystyle 1}$ when ${\displaystyle nth}$ ${\displaystyle {\textrm {prime}}>i}$ ${\displaystyle \Rightarrow {i}<{nth}}$ ${\displaystyle {\textrm {prime}}}$

${\displaystyle 0}$ when ${\displaystyle nth}$ ${\displaystyle {\textrm {prime}}\leq {i}}$ ${\displaystyle \Rightarrow {i}\geq {nth}}$ ${\displaystyle {\textrm {prime}}}$

${\displaystyle \sum _{i=1}^{2^{n}}{\lfloor ({\frac {n}{\sum _{j=1}^{i}{\lfloor (\cos {\pi {\frac {{(j-1)!}+j}{j}}})^{2}\rfloor }}})^{\frac {1}{n}}\rfloor }}$ gives the number ${\displaystyle -1}$, and the ${\displaystyle -1}$ is because when ${\displaystyle i<{nth}}$ ${\displaystyle {\textrm {prime}}}$ reaches ${\displaystyle i={nth}}$ ${\displaystyle {\textrm {prime}}}$, the function doesn't add 1. The formula adds up to ${\displaystyle 2^{n}}$ is because Bertrand's postulate says ${\displaystyle 2^{n}}$ is bigger than the nth prime number.

And finally, ${\displaystyle 1}$ is added because of the ${\displaystyle -1}$.[1]

## References

1. An Exact Formula for the Primes: Willans' Formula, retrieved 2022-11-01