Euler'totient function

Keywords: Python less Google

Euler'totient function

  • Author: Jasper Yang

  • School: Bupt

Preface

The derivation of gamma function will appear so-called Euler function (phi). In a paper, I need to evaluate several Euler functions. The results are not comprehensible. I go to google immediately and find an open source python library to calculate Euler function.

class eulerlib.numtheory.Divisors(maxnum=1000)
    Implements methods related to prime factors and divisors.

    Parameters:    maxnum – Upper limit for the list of primes. (default = 1000)
    divisors(num)
        Returns a list of ALL divisors of num (including 1 and num).
        
        Parameters:    num – An integer for which divisors are needed.
        Returns:    A list [d1,d2,...dn] of divisors of num
    
    phi(num)
        Returns the number of totatives of num

        Parameters:    num – Integer for which number of totatives are needed.
        Returns:    Number of totatives of num

Note A totative of an integer num is any integer i such that, 0 < i < n and GCD(i,num) == 1.
Uses Euler's totient function.

This function can't understand the usage and meaning at this point. Now I will introduce two concepts to let you slowly understand the process.

Totative

from wiki

In number theory, the totative of a given n is a k that conforms to greater than 0 and less than or equal to n, and the K and N are reciprocal prime numbers (what is a reciprocal prime number).

Mutual prime number is a concept in mathematics, that is, the common factor of two or more integers is only a non-zero natural number. Common factor has only one or two non-zero natural numbers, called reciprocal prime numbers.

Euler equation $$ phi(x) $is to calculate the number of totations of n.
totatives under the multiplication module of N form the multiplication group of integers modulo n. I didn't understand the group knowledge in the latter sentence after I got to Wikipedia. I gave it up. In the future, I will have a chance to see the Chinese material and understand it before adding it. wiki portal

Euler's totient function

This is the protagonist Euler function.

from wiki

In number theory, for positive integers n, the Euler function $\ varphi (n) $is the number of numbers that are mutually prime with N in positive integers less than or equal to n. This function is named after its first researcher, Euler. It is also called a function (named by Gauss) or a totient function (named by Silvester).
For example, $ varphi (8)=4 $$, because 1, 3, 5, 7 and 8 are mutually prime.
Euler function is actually the order of multiplication group consisting of congruent classes of modulus n (that is, multiplication group consisting of all units of ring ${ mathbb {Z}/ n { mathbb {Z} $$). This property, together with Lagrange's theorem, constitutes the proof of Euler's theorem.

If n is the K power of prime p, $ varphi (n) = varphi (p ^ {k}) = P ^ {k} - P {{k-1}= (p-1) p ^ {k-1}$, because all the numbers except the multiple of P are mutually prime with n.

If $$n = P {1} ^ {k {1}} P {2} ^ {k {2} _ cdots P {r} {k {r}}$$

Then $\varphi (n) $$\varphi (n) = \prod {{{{i=1}} ^{{{{{i}{{{{{{k {{{i}-1} {{{{{{{{i}-1) = \prod {{{{{{{{p\midmidn}}}}{{{{{{{\\\\midmidn}}}}}}}}}}}}}}{{{{{{{{\ left (1-{ frac {1} {p} right)$$
Among them, $ alpha {p}$is the largest integer $alpha $dividing $$p ^ {alpha} $$by n (here $\ alpha {p {i}= K {i}$).

For example, $ varphi (72) = varphi (2 ^ {3} times 3 ^ {2}) = 2 ^ {3-1} (2-1)\ times 3 ^ {2-1} (3-1) = 2 {2}\ times\ 1\ times 2 \ times 2 = 24$$

My understanding

Why there are two laws, one is basic calculation and the other is continuous multiplication, in fact, because it is thought that all numbers can be decomposed into the form of k power of two prime numbers.

The above knowledge is enough for me. If you need more understanding, see the link below.

Eulerlib

This is an open source python language implementation library
We mainly use the inside ones.

Under eulerlib. num theory. Divisors (maxnum = 1000)

phi function
Use process,
e = eulerlib.numtheory.Divisors(10000) # where 10000 is the maximum, default 1000
 e.phi(100)# Seek phi(100)

It's very simple to use.

The source code of this function is as follows: Source Port

    def phi(self,num):
        """Returns the number of `totatives 
        <http://en.wikipedia.org/wiki/Totative>`_ of *num*
        
        :param num: Integer for which number of totatives are needed.
        :returns: Number of totatives of *num*
        
        .. note::
        
            A totative of an integer *num* is any integer *i* such that,
            0 < i < n and *GCD(i,num) == 1*.
        
        Uses `Euler's totient function 
        <http://en.wikipedia.org/wiki/Euler%27s_totient_function>`_.
        """
        if(num < 1):
            return 0
        if(num == 1):
            return 1
        if(num in self.primes_table):    # This table of prime numbers has been in place since the very beginning. Derived from other packages, it is defined as all prime numbers within maxnum.
            return num-1
        pfs = self.prime_factors_only(num) # This step is to find p.
        prod = num
        for pfi in pfs:
            prod = prod*(pfi-1)/pfi
        return prod



    
    def prime_factors_only(self,num):
        """Returns the `prime factors 
        <http://en.wikipedia.org/wiki/Prime_factor>`_ *pf* :sub:`i` of *num*.
        
        :param num: An integer for which prime factors are needed
        :returns: A list [pf1,pf2,...pfi] of prime factors of *num*
        """
        if num in self.pfactonly_table:
            return self.pfactonly_table[num]
        elif ((num < 2) or (num > self.limit)):
            return []
        elif num in self.primes_table:
            self.pfactonly_table[num] = [num]
            return [num]
        else:
            result = []
            tnum = num
            for prime in self.primes_table:
                if(tnum%prime==0):
                    result.append(prime)
                    pdiv = prime*prime
                    while(tnum%pdiv == 0):
                        pdiv *= prime
                    pdiv //= prime # This /= and /= do not seem to differ.
                    tnum //= pdiv
                    if(tnum in self.primes_table):
                        result.append(tnum)
                        break
                    elif(tnum == 1):
                        break
            self.pfactonly_table[num] = result
            return result

Source code also looks very simple and easy to understand, in order to find out p1 and p2, and then you can calculate phi values and multiply them.

paper done : 2017/4/19

Posted by bestrong on Tue, 11 Jun 2019 15:27:52 -0700