Below is an example of a subordinate part of the Willans’ Formula responsible for calculating the primes within a given range, implemented as a function in R. Willans’ formula, introduced by Willan in 1964, is an elegant prime-generating formula that exploits the properties of prime numbers and certain mathematical functions to identify primes.

The formula is defined as follows:

The function F(j) is equal to the floor of the square of the cosine of the value pi((j-1)!+1)/j (where ! denotes the factorial operation, and pi is the constant Pi). Specifically:

The floor function ⌊x⌋ rounds x down to the greatest integer less than or equal to x. The values of F(j) are such that:

This part of the formula is a consequence of Wilson’s theorem, and it hides the prime numbers j as those for which F(j) = 1.

Then, the prime counting function π(x) is defined as:

And the n-th prime number p_n is given by:

This can also be written as:

Here, π(m) is the prime-counting function, Σ denotes the summation, and n is the number of primes.

Whilst this is not a particularly efficient or practical formula when considering scalability, it still manages to be a beautiful interplay of a variety of clever mathematical concepts.

Formula with sigma notation:

Willans’ formula in its entirety: