Finding large prime numbers is an essential part of the mechanisms involved in Internet security and privacy. And not just prime numbers, sometimes you need to perform various calculations on numbers that are specifically not prime. There are also uses of methods you have familiarized yourself with during your childhood, such as greatest common divisor (GCD) and least common multiple (LCM).
Based on these, in 1763 Carl Friedrich Gauss solved this interesting problem:
If you are given a number N, you have to determine how many integers between 1 and N have a GCD of 1 with N.
Later in this tutorial we will see that the solution of this problem can be written as . Knowing how Sieve of Eratosthenes works and this tutorial we will solve this about 250 years old problem.
If you are given two numbers x and y, then their common divisor is a number z such that x and y are both divisible by z. And GCD is the largest number among all of the common divisors of x and y.
If you are given 8 and 12, the common divisors of these two numbers are 1, 2 and 4. Here, 4 is the largest and so 4 is the greatest common divisor of 8 and 12.
Now if the GCD of x and y is 1, then x and y are co-prime numbers. For example, 9 and 14, and 21 and 52 are some examples of co-prime numbers.
Then in Gauss' problem, if N is a prime number, how many of the numbers between 1 and N are co-prime? Suppose N is 7, which is a prime number; then if you think about it, all of the numbers between from 1 to 6 are co-prime with 7. This is true for all prime numbers.
Therefore, if N is a prime number, then:
What about prime powers? Let's say N is 125 which is a prime power of 5 (53). Determining the number of co-primes for 125 is a bit tricky. But may be it would be easier to answer which numbers are not co-prime with 125.
A few of such numbers would be 5, 10, 15, 20, 25, etc. These numbers are not co-prime with 125 because the GCD of these numbers with 125 are greater than 1. And this can only happen when there exists common prime factors of both numbers. For example, here is the prime factorization of 125 and 50 (and they are co-prime with each other):
Here you can see both of these numbers have 5 as their prime factor. And so their GCD will be greater than 1. Any prime power, when factorized, will always yield one prime number as its prime factor.
Therefore, for any number to have GCD with 125 greater than 1 it must have 5 among its prime factors. In other words, if N = P^x and P is a prime number, then P must be a prime factor of all numbers for which the GCD with N is greater than 1.
Now to determine how many whole numbers from 1 to 125 are not co-prime with 125, we need to determine how many numbers in that range are divisible by 5. And, to do that, we need to divide 125 by 5. There are 25 numbers from 1 to 125 that are divisible by 5, and that means there are 25 numbers in that range that are not co-prime with 125.
Then how many numbers are co-prime with 125? Easy: 125 - 25 = 100.
Euler's Totient function (also known as Phi Function) gives us the number of co-primes of N that are smaller than N. Here are some examples:
Based on the discussion from the last section, we can say that for a number N such that N = P^x, the Totient function would be as follows:
Let's break this down.
There are P^x whole numbers between 1 and P^x. Of which, there numbers that have a GCD with N greater than 1. Therefore, we can just subtract this number from the total number of whole numbers in the range to find the value of .
But, not all numbers are prime powers. What can we do about those numbers? Let's take a number that is a product of two prime numbers. For example, 35 and 55.
How did we find the value of for these numbers?
Let's say that we have two prime numbers, p and q. And, we want to find the value of . Now, for any number x to have GCD with greater than 1, there must be either p or q in the prime factorization of x.
Now, if x is smaller than there x can never have both p and q in its prime factorization.
Therefore we can say the following things with certainty:
$ \frac{p \times q}{p} = q
p \times q $` that are divisible by p.$ \frac{p \times q}{q} = p
p \times q $` that are divisible by q.Therefore,
Since we already know that,
Therefore,
Notice that if two numbers are prime, they will definitely be co-prime. But if one of them is not prime, then . We will discuss why this is true in a later tutorial. Now we have what we need to find the of any number.
Any whole number, except 1, can be written as a product of prime powers. This done through prime factorization of the number. For example, the prime factorization of 720 is:
Generally, prime factorization of a number has the following form:
Here each P is a prime number, and each x is its power.
Suppose and
Then, N can be written as . Notice that there are no common prime numbers in S and T. Therefore, the GCD of S and T is 1 and they are co-prime. Which means:
As S is a prime power of , we can write .
Therefore,
Similarly, we can break T into the form of , where and
Here, U and V are co-prime and so,
Which makes
We can now generalize to:
Since we know , then:
By figuring out the prime factorization of a number, we can follow this formula to easily determine the value of for that number.
If you know how to use Sieve of Eratosthenes to find prime numbers, then you can use a very similar method to determine the value of for all numbers between 1 and N.
Here is the pseudo code that calculates the value of for all whole numbers between 1 and N:
function totient(n)
let phi = array of length n
for i = 0 to n
phi[i] = i
for h = 2 to n
if phi[h] == h
for x = multiples of h up to n (i.e. 2*h, 3*h, 4*h, ....)
phi[x] = (phi[x] / h) * (h - 1)
Sieve of EratosthenesA beginner's number theory tutorial on Sieve of Eratosthenes. |