The only formula guaranteed to generate prime numbers is as follows:
A prime p is called primorial or prime-factorial if it has the form p = n# ± 1 for some number n, where n# stands for the product of all the primes ≤ n.
There are various other common forms of primes, but these formulas will only generate prime numbers for certain values of n. Other values generate non-prime numbers.
A prime is called factorial if it is of the form n! ± 1. The first factorial primes are:
n! − 1 is prime for n = 3, 4, 6, 7, 12, 14, 30, 32, 33, 38, 94, 166,...
n! + 1 is prime for n = 0, 1, 2, 3, 11, 27, 37, 41, 73, 77, 116, 154...
Primes of the form 2^n − 1 are known as Mersenne primes, while primes of the form 2^(2^n) + 1 are known as Fermat primes. Prime numbers p where 2p + 1 is also prime are known as Sophie Germain primes. The following list is of other special types of prime numbers that come from formulas:
Wieferich primes,
Wilson primes,
Wall-Sun-Sun primes,
Wolstenholme primes,
Unique primes,
Newman-Shanks-Williams primes (NSW primes),
Smarandache-Wellin primes,
Wagstaff primes, and
Supersingular primes.