Implementation of Euler function (C language)

 Let’s look at an example first:

 The previous words are actually not helpful in solving the problem. This question actually requires us to directly output the result of n*m*φ(n)*φ(m). where φ(n) is the Euler function.

First let’s look at what the Euler function is:

1. What is the Euler function
The Euler function refers to: for a positive integer n, the number of positive integers that are relatively prime to n among the positive integers less than or equal to n. (including 1), denoted as φ ( n )

2. Some properties of the Euler function
If n is a prime number, then φ ( n ) = n - 1;
If m and n Reciprocally prime, then φ ( n*m ) = φ ( n ) * φ ( m );
If the positive integers n and a are relatively prime, then there is
If n is an odd number, φ ( 2n ) = φ ( n );
If n = pk and p is a prime number, then φ ( n ) = (p - 1) * pk-1 = pk - pk-1.


Original link: https://blog.csdn.net/weixin_45843077/article/details/108741889

Let’s take a look at what coprime numbers are;

Coprime numbers mean that the greatest common divisor of two numbers is only 1. For example, the greatest common divisor of 6 and 3 is 3, and they are not mutually prime numbers; the greatest common divisor of 10 and 9 is 1, and they are mutually prime numbers.

So how to prove whether two numbers are coprime:

What I use here is EuclideanEuclidean division method

The general idea is:

1 First, let r be the remainder obtained by a/b, that is, r = a % b (0≤r<b)

 2 If r= 0, the program ends; b is the greatest common divisor.

3 If r!= 0: Then assign b to a, assign r to b, and return to the first step.

int fun(int m,int n)
{
    int mod;
    mod=m%n;
    while(mod!=0)
    {
        a=b;
        b=mod;
        mod=a%b;
    }
    return n;
}

 

Next is the entire code snippet


#include<iostream>
#include<math.h>
#define size 1000
using namespace std;
int zhishu(int n)//判断是否为质数
{
    int flag=0;
    for(int i=2; i<sqrt(n); i++)
    {
        if (n%i==0)
            flag++;
    }
    if(n!=1&&flag==0)
        return 1;//是质数
    else
        return 0;
}
int func(int n)//欧拉函数的实现函数
{
    if(zhishu(n))
        return n-1;
    int sum=0;
    for(int j=1; j<n; j++)//欧几里得算法
    {
        int m=n;
        int i=j;
        int mod;//余数;
        while(i!=0)
        {
            mod=m%i;
            m=i;
            i=mod;
        }
        if(m==1)
            sum++;
    }
    return sum;
}
int main()
{
    long long l,r;
    cin>>l>>r;
    cout<<l*r*func(l)*func(r);
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_46713492/article/details/125495531