Primes!

I am sorry if it can not load properly on your device.

Prime numbers are essential building blocks in number theory and have important applications in mathematics, cryptography, and computer science. Let’s dive deeper into what prime numbers are, their properties, methods to find them, and some interesting facts.

What Are Prime Numbers?

A prime number is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. In simpler terms, a prime number has exactly two distinct positive divisors: 1 and itself.

Example

Take the number 7. The only divisors of 7 are 1 and 7. Hence, 7 is a prime number. However, 9 has divisors 1, 3, and 9, making it not a prime number.

Properties of Prime Numbers

How to Find Prime Numbers

There are several ways to find prime numbers, depending on the size of the number and the purpose of the search. Here are some common methods:

1. Trial Division

One of the simplest methods is to divide a number by all primes smaller than its square root. If none divide it evenly, the number is prime.

To check if 29 is prime:

2. The Sieve of Eratosthenes

The Sieve of Eratosthenes is an ancient algorithm to find all primes up to a specified limit. It works by iteratively marking the multiples of each prime number starting from 2. The numbers that remain unmarked are primes.

Steps of the Sieve Algorithm

  1. Create a list of consecutive integers from 2 to a desired limit, say 30.
  2. Start with 2 (the first prime). Mark all multiples of 2 (4, 6, 8, etc.).
  3. Move to the next unmarked number (3). Mark all multiples of 3 (6, 9, 12, etc.).
  4. Repeat this process with the next unmarked number (5, then 7, etc.) until you reach the square root of the limit.
  5. The remaining unmarked numbers are primes: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29.

3. Fermat Primality Test

The Fermat primality test is a probabilistic test used to determine if a number is likely to be prime. If a number passes this test for several values, it is probably prime. However, there are exceptions called Carmichael numbers that can trick the test.

It says that a number is a prime if \(a^n \equiv a \pmod n\)

4. The Miller-Rabin Test

The Miller-Rabin test is an advanced algorithm that improves on Fermat's test. It is often used in practice to check for primality, especially when working with large numbers in cryptography. It is also probabilistic, but less likely to produce false positives.

Steps of the Miller-Rabin Test

  1. Write \( n-1 \) as \( 2^s \cdot d \) where \( d \) is odd.
  2. Pick a random base \( a \) such that \( 2 \leq a \leq n-2 \).
  3. Check the base condition by computing \( x = a^d \mod n \). If \( x = 1 \) or \( x = n-1 \), the number passes this round.
  4. For \( r = 1 \) to \( s-1 \), square \( x \) and compute \( x = x^2 \mod n \). If \( x = n-1 \), the number passes this round.
  5. If no condition is met, \( n \) is composite.

Example

Let’s say we want to test whether \( n = 561 \) is prime.

Python code for the Miller-Rabin Test In SymPy ntheory primetest.py

        def mrtest(n, base, s, t):
        """Miller-Rabin strong pseudoprime test for one base.
        Return False if n is definitely composite, True if n is
        probably prime, with a probability greater than 3/4.
    
        """
        # do the Fermat test
        b = pow(base, t, n)
        if b == 1 or b == n - 1:
            return True
        else:
            for j in range(1, s):
                b = pow(b, 2, n)
                if b == n - 1:
                    return True
                # see I. Niven et al. "An Introduction to Theory of Numbers", page 78
                if b == 1:
                    return False
        return False
    

In this Python implementation, \( n \) is the number to test, and \( base \) is the base. The algorithm returns True if \( n \) is "probably prime" and False if it is composite.

5. p+1 or p-1 tests

The test is very fast when the tested number plus or minus 1 is very easy to factor.

6. AKS Primality Test

The AKS primality test is a relatively recent discovery that can determine with certainty whether a number is prime in polynomial time. Although the algorithm is theoretically important, it is not practical for very large numbers due to its complexity.

Applications of Prime Numbers

Prime numbers have numerous practical applications, particularly in fields related to computer science and cryptography:

Interesting Mathematical Curiosities

My prime-search project here.