To determine whether a number is prime, one can test divisibility by candidate factors up to its square root. If any division by an integer greater than yields a zero remainder, then the number is not prime. Otherwise, it is prime.
To detect when a quotient is an integer, we introduce the function , defined for any integer . This function returns when , and otherwise:
When , the denominator is greater than , and the expression evaluates to . When , the value is . The function could also be defined for real values of and , and would behave in the same way, but this is not necessary for our purposes.
We now define a function that checks whether has any nontrivial integer factor. For each integer , we compute the remainder of the division of by , given by , and apply :
For , all factors in this product are equal to when is prime, so . If is composite, at least one factor is , which makes .
For , the product is empty and therefore equal to . Since is not prime, we introduce a correction factor:
This expression is zero only when . We can now define a function that indicates primality:
This function returns if is prime and otherwise.
The prime-counting function is then given by:
We also define , corresponding to the empty sum.
Given an upper bound for the -th prime, we can construct a function that returns the -th prime by summing candidate values , weighted by a selector that picks out the first such that :
This works because the -th prime is precisely the value of for which and .
Since grows approximately as , we can use upper bounds such as:
The first bound relies on the fact that is less than for , with smaller cases verifiable directly. The last grows very rapidly and serves as a guaranteed bound when no good estimate for is available.
This formula is not computationally efficient. It is simply a mathematical reformulation of the classical primality test based on successive divisions up to the square root of the number being tested.
This formula was created (discovered?) on 2026-05-04 by José Eduardo Gaboardi de Carvalho (edu.a1978 at gmail).