Euler's Theorem
We're going to briefly look at Euler's totient function, .
It is defined as:
For ,
Note: We can also use the interval because is not relatively prime to anything.
In other words, is the number of all positive integers up to that are relatively prime to .
We can also say that if and are relatively prime, then .
As we've seen in the previous section, it also means that it'll be the numbers that have inverses and are cancellable .
Let's call this set , and write it as: .
Then, .
A simple example is .
All positive integers up to () are relatively prime to .
Another example is . All positive integers that are relatively prime to in that interval are , and .
If is prime, everything in is relatively prime to .
Therefore, when is prime, , as we've seen with .
Let's look at another example, .
We can also write it as a power of a prime, .
Here is the reasoning: a number is relatively prime to iff it is relatively prime to . Because divides every third number in that interval (, , 3, , , 6, , ), every third number will not be relatively prime to .
So, it will be the set of all numbers minus of :
.
We can generalize, and say that of the numbers in that interval will not be relatively prime to .
Therefore, .
To write it more simply:
What if the number we're dealing with is not a power of a prime, like as we've just seen?
One fact is that if and are relatively prime, then
This is called multiplicativity; a function is multiplicative when its value at a product of relatively prime numbers is the product of the values at those two relatively prime numbers.
Again, if English is confusing, here is the same thing put more precisely: .
That means is a multiplicative function.
Okay, let's go back to . We can now write it as . That also means, .
Our job is easy, is a prime already, and we can write as a power of a prime, .
If we use the formulas we've just seen, then we can write the whole thing as:
.
That means , which is .
So, — the size of the set . And, we're done.
Isn't it elegant?
So, to summarize, aside from when is a prime, we've seen two other rules:
If is a prime, then
If and are relatively prime, then
Let's look at a wonderful thing.
We used example, the result was as it was the size of the set of those that are relatively prime to : .
We called this set .
Now, if we take any number from , and multiply it by each number in , we'll still get the same set (), but in a different order:
So, in our example, is .
Let's pick any , for example, .
Multiplying each item in , we have , and of course, since it has to be in , it is the set . It is the same set!
We can try it with a different number in the set, say .
This time we have , and since this is , it is . The same set again!
In terms of congruence, we can also say that if and are relatively prime, then:
Let's say that is , and is .
Those that are relatively prime to less than is the set . Its size is , so :
And, it is correct:
Fermat's Little Theorem states,
So, for example, the remainder of dividing by will be — that is, , or .
For primes and ,
A simple example, let's say is , and is .
So, this has to be true: .
The set of those relatively prime to that are less than is .
Indeed, .
An example. We're asked
How many numbers between and (inclusive) are relatively prime to ?
We know that is not a prime. One thing to do is to either write as a power of a prime, or write it as the product of two numbers that are relatively prime.
We can write it as the product of and .
Then, we can solve this using the gcd
function we used before, and newly defined ring
and phi
functions:
def gcd(x, y):
if y == 0:
return x
return gcd(y, x % y)
def ring(n):
return [i for i in range(1, n) if gcd(i, n) == 1]
def phi(n):
return len(ring(n))
a = 7
b = 540
print(phi(a) * phi(b))
The answer is indeed .