# Prime Number Prime number (or just *prime*) is a [whole](integer.md) positive [number](number.md) only divisible by 1 and itself, except for the number [1](one.md). I.e. prime numbers are 2, 3, 5, 7, 11, 13, 17 etc. Prime numbers are extremely important, [interesting](interesting.md) and mysterious for their properties and distribution among other numbers, they have for millennia fascinated [mathematicians](math.md), nowadays they are studied in the math subfield called [number theory](number_theory.md). Primes are for example essential in [assymetric cryptography](assymetric_cryptography.md). The largest known prime number as of 2022 is 2^82589933 - 1 (it is so called [Mersenne prime](mersenne_prime.md), i.e. a prime of form 2^N - 1). Every natural number greater than 1 has a unique **prime factorization**, i.e. a [set](set.md) of prime numbers whose product it is. For example 75 is a product of three primes: 3 * 5 * 5. This is called the *fundamental theorem of arithmetic*. Naturally, each prime has a factorization consisting of a single number -- itself -- while factorizations of non-primes consist of at least two primes. To mathematicians prime numbers are what chemical elements are to chemists. **Why is 1 not a prime?** Out of convenience -- if 1 was a prime, the fundamental theorem of arithmetic would not hold because 75's factorization could be 3 * 5 * 5 but also 1 * 3 * 5 * 5, 1 * 1 * 3 * 5 * 5 etc. The unique factorization can also nicely be used to encode [multisets](multiset.md) as numbers. We can assign each prime number its sequential number (2 is 0, 3 is 1, 5 is 2, 7 is 3 etc.), then any number encodes a set of numbers (i.e. just their presence, without specifying their order) in its factorization. E.g. 75 = 3 * 5 * 5 encodes a multiset {1, 2, 2}. This can be exploited in cool ways in some [cyphers](cypher.md) etc. **There are infinitely many prime numbers**. The proof is pretty simple (shown below), however it's pretty interesting that it has still not been proven whether there are infinitely many [twin primes](twin_prime.md) (primes that differ by 2), that seems to be an extremely difficult question. Euklid's [proof](proof.md) shows there are infinitely many primes, it is done by contradiction and goes as follows: suppose there are finitely many primes *p1*, *p2*, ... *pn*. Now let's consider a number *s* = *p1* * *p2* * ... * *pn* + 1. This means *s* - 1 is divisible by each prime *p1*, *p2*, ... *pn*, but *s* itself is not divisible by any of them (as it is just 1 greater than *s* and multiples of some number *q* greater than 1 have to be spaced by *q*, i.e. more than 1). If *s* isn't divisible by any of the considered primes, it itself has to be a prime. However that is in contradiction with the original assumption that *p1*, *p2*, ... *pn* are all existing primes. Therefore a finite list of primes cannot exist, there have to be infinitely many of them. **Distribution and occurrence of primes**: the occurrence of primes seems kind of """[random](random.md)""" (kind of like digits of [decimal](decimal.md) representation of [pi](pi.md)), without a simple pattern, however hints of patterns appear such as the [Ulam spiral](ulam_spiral.d) -- if we plot natural numbers in a square spiral and mark the primes, we can visually distinguish dimly appearing 45 degree diagonals as well as horizontal and vertical lines. Furthermore the **density of primes decreases** the further away we go from 0. The *prime number theorem* states that a number randomly chosen between 0 and *N* (for large *N*) has approximately 1/log(N) probability of being a prime. **Prime counting function** is a function for *N* tells the number of primes smaller or equal to *N*. While there are 25 primes under 100 (25%), there are 9592 under 100000 (~9.5%) and only 50847534 under 1000000000 (~5%). ``` , , , ', ' ,' , , ' ',', ' , ' ', ' ', ,, , ,' ' ', ,' ,' , ' ' , , ', , ' ,' ', ,' ' ,', , ' ',', , , ', ', , , , ,' ,' ' ', , , ' ,', ,', ' , , ,' ' , ' , , ', ' ', , ,' , ', ' ' ' , ',', , ,' ' , ,' ' ' ,',' ', , , ' ' ' ', , , , ', , ', , ' ' ' ' ' ', , ,' ' ' ', ' ', ', , , ,', ,',', ,' ' ' , ' ' ' , , , , ,',;,', , , , ,', , ' ' ' ',' ', ,,' ' , ' ,' ', ',' ', ' ', ,' ,' ' ', , , , , ' , , ,' , , ' ' ', ', ' , , , ' ' ' ,, , , , , ' ' ,', ', ' , ,' ,',' , , ,', ' , , ',', , , ', ' ' ', , , ,' ', ', , ', , ,' ', ' ' , ' ' , ' ' ' ' ', ', , , ' ' ' ' ,' , , ,' ', ,' ', , ,', , ' , , ' ' , ', , , , , ,' ', ``` *Ulam spiral: the center of the image is the number 1, the number line continues counter clockwise, each point represents a prime.* Here are prime numbers under 1000: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997. ``` ______/ / / _____ ______/_ / ____ / X /__\ / ___ / \/__ / \ /__ \/ / \ / /\ \ / \ / \ /\ --2--3--/--5--X--7--/--\--X--11-X--13-X--\-- \__\/ \ \/ \__\/ \ \/ \__\/ \ \/ \__\/ \__\_/\ \ /\ \ /\__\_/\ \ /\ \ /\ \__\__X \ X \ X \ X__\__X \__\__\/ \ \/ \ \/ \ \/ \ \__\__\_/\ \ /\ \ /\ \ ``` ## Algorithms **Primality test**: testing whether a number is a prime is quite easy and not computationally difficult (unlike factoring the number). A [naive](naive.md) algorithm is called *trial division* and it tests whether any number from 2 up to the tested number divides the tested number (if so, then the number is not a prime, otherwise it is). This can be optimized by only testing numbers up to the [square root](sqrt.md) (including) of the tested number (if there is a factor greater than the square root, there is also another smaller than it which would already have been tested). A further simple optimization is to to test division by 2, 3 and then only numbers of the form 6q +- 1 (other forms are divisible by either 2 or 3, e.g 6q + 4 is always divisible by 2). Further optimizations exist and for maximum speed a [look up table](lut.md) may be used for smaller primes. A simple [C](c.md) function for primality test may look e.g. like this: ``` int isPrime(int n) { if (n < 4) return n > 1; if (n % 2 == 0 || n % 3 == 0) return 0; int test = 6; while (test <= n / 2) // replace n / 2 by sqrt(n) if available { if (n % (test + 1) == 0 || n % (test - 1) == 0) return 0; test *= 6; } return 1; } ``` [Sieve of Eratosthenes](sieve_of_eratosthenes.md) is a simple algorithm to find prime numbers up to a certain bound *N*. The idea of it is following: create a list of numbers up to *N* and then iteratively mark multiples of whole numbers as non-primes. At the end all remaining (non-marked) numbers are primes. If we need to find all primes under *N*, this algorithm is more efficient than testing each number under *N* for primality separately (we're making use of a kind of [dynamic programming](dynamic_programming.md) approach). Prime factorization: TODO Prime generation: TODO ## See Also - [happy number](happy_number.md)