📜  检查给定数字的Euler Totient函数是否相同以及该数字的两倍

📅  最后修改于: 2021-04-22 00:22:48             🧑  作者: Mango

给定整数N ,任务是检查N2 * N的欧拉Totient函数是否相同。如果发现它们相同,则打印“是” 。否则,打印“否”

例子:

朴素方法:最简单的方法是找到N2 * N的欧拉Totient函数。如果它们相同,则打印“是”。否则,打印“否”。

下面是上述方法的实现:

C++
// C++ program of the above approach
 
#include 
using namespace std;
 
// Function to find the Euler's
// Totient Function
int phi(int n)
{
    // Initialize result as N
    int result = 1;
 
    // Consider all prime factors
    // of n and subtract their
    // multiples from result
    for (int p = 2; p < n; p++) {
        if (__gcd(p, n) == 1) {
            result++;
        }
    }
 
    // Return the count
    return result;
}
 
// Function to check if phi(n)
// is equals phi(2*n)
bool sameEulerTotient(int n)
{
 
    return phi(n) == phi(2 * n);
}
 
// Driver Code
int main()
{
    int N = 13;
    if (sameEulerTotient(N))
        cout << "Yes";
    else
        cout << "No";
}


Java
// Java program of
// the above approach
import java.util.*;
class GFG{
 
// Function to find the Euler's
// Totient Function
static int phi(int n)
{
  // Initialize result as N
  int result = 1;
 
  // Consider all prime factors
  // of n and subtract their
  // multiples from result
  for (int p = 2; p < n; p++)
  {
    if (__gcd(p, n) == 1)
    {
      result++;
    }
  }
 
  // Return the count
  return result;
}
 
// Function to check if phi(n)
// is equals phi(2*n)
static boolean sameEulerTotient(int n)
{
  return phi(n) == phi(2 * n);
}
   
static int __gcd(int a, int b) 
{ 
  return b == 0 ? a :__gcd(b, a % b);    
}
   
// Driver Code
public static void main(String[] args)
{
  int N = 13;
  if (sameEulerTotient(N))
    System.out.print("Yes");
  else
    System.out.print("No");
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program of the
# above approach
 
# Function to find the Euler's
# Totient Function
def phi(n):
     
    # Initialize result as N
    result = 1
 
    # Consider all prime factors
    # of n and subtract their
    # multiples from result
    for p in range(2, n):
        if (__gcd(p, n) == 1):
            result += 1
 
    # Return the count
    return result
 
# Function to check if phi(n)
# is equals phi(2*n)
def sameEulerTotient(n):
     
    return phi(n) == phi(2 * n)
 
def __gcd(a, b):
     
    return a if b == 0 else __gcd(b, a % b)
 
# Driver Code
if __name__ == '__main__':
     
    N = 13
     
    if (sameEulerTotient(N)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by Amit Katiyar


C#
// C# program of
// the above approach
using System;
class GFG{
 
// Function to find the Euler's
// Totient Function
static int phi(int n)
{
  // Initialize result as N
  int result = 1;
 
  // Consider all prime factors
  // of n and subtract their
  // multiples from result
  for (int p = 2; p < n; p++)
  {
    if (__gcd(p, n) == 1)
    {
      result++;
    }
  }
 
  // Return the count
  return result;
}
 
// Function to check if phi(n)
// is equals phi(2*n)
static bool sameEulerTotient(int n)
{
  return phi(n) == phi(2 * n);
}
   
static int __gcd(int a, int b) 
{ 
  return b == 0 ? a : __gcd(b, a % b);    
}
   
// Driver Code
public static void Main(String[] args)
{
  int N = 13;
  if (sameEulerTotient(N))
    Console.Write("Yes");
  else
    Console.Write("No");
}
}
 
// This code is contributed by shikhasingrajput


C++
// C++ program of the above approach
 
#include 
using namespace std;
 
// Function to check if phi(n)
// is equals phi(2*n)
bool sameEulerTotient(int N)
{
    // Return if N is odd
    return (N & 1);
}
 
// Driver Code
int main()
{
    int N = 13;
 
    // Function Call
    if (sameEulerTotient(N))
        cout << "Yes";
    else
        cout << "No";
}


Java
// Java program of the above approach
class GFG{
 
// Function to check if phi(n)
// is equals phi(2*n)
static int sameEulerTotient(int N)
{
     
    // Return if N is odd
    return (N & 1);
}
 
// Driver code
public static void main(String[] args)
{
    int N = 13;
 
    // Function call
    if (sameEulerTotient(N) == 1)
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
 
// This code is contributed by Dewanti


Python3
# Python3 program of the above approach
 
# Function to check if phi(n)
# is equals phi(2*n)
def sameEulerTotient(N):
     
    # Return if N is odd
    return (N & 1);
 
# Driver code
if __name__ == '__main__':
     
    N = 13;
 
    # Function call
    if (sameEulerTotient(N) == 1):
        print("Yes");
    else:
        print("No");
 
# This code is contributed by Amit Katiyar


C#
// C# program of
// the above approach
using System;
class GFG{
 
// Function to check if phi(n)
// is equals phi(2*n)
static int sameEulerTotient(int N)
{
  // Return if N is odd
  return (N & 1);
}
 
// Driver code
public static void Main(String[] args)
{
  int N = 13;
 
  // Function call
  if (sameEulerTotient(N) == 1)
    Console.Write("Yes");
  else
    Console.Write("No");
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
Yes

时间复杂度: O(N)
辅助空间: O(1)

高效方法:要优化上述方法,主要观察结果是,无需计算欧拉Totient函数,因为只有奇数遵循属性phi(N)= phi(2 * N) ,其中phi()是欧拉函数Totient函数。

因此,想法是检查N是否为奇数。如果发现是真的,则打印“是”。否则,打印“否”。

下面是上述方法的实现:

C++

// C++ program of the above approach
 
#include 
using namespace std;
 
// Function to check if phi(n)
// is equals phi(2*n)
bool sameEulerTotient(int N)
{
    // Return if N is odd
    return (N & 1);
}
 
// Driver Code
int main()
{
    int N = 13;
 
    // Function Call
    if (sameEulerTotient(N))
        cout << "Yes";
    else
        cout << "No";
}

Java

// Java program of the above approach
class GFG{
 
// Function to check if phi(n)
// is equals phi(2*n)
static int sameEulerTotient(int N)
{
     
    // Return if N is odd
    return (N & 1);
}
 
// Driver code
public static void main(String[] args)
{
    int N = 13;
 
    // Function call
    if (sameEulerTotient(N) == 1)
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
 
// This code is contributed by Dewanti

Python3

# Python3 program of the above approach
 
# Function to check if phi(n)
# is equals phi(2*n)
def sameEulerTotient(N):
     
    # Return if N is odd
    return (N & 1);
 
# Driver code
if __name__ == '__main__':
     
    N = 13;
 
    # Function call
    if (sameEulerTotient(N) == 1):
        print("Yes");
    else:
        print("No");
 
# This code is contributed by Amit Katiyar

C#

// C# program of
// the above approach
using System;
class GFG{
 
// Function to check if phi(n)
// is equals phi(2*n)
static int sameEulerTotient(int N)
{
  // Return if N is odd
  return (N & 1);
}
 
// Driver code
public static void Main(String[] args)
{
  int N = 13;
 
  // Function call
  if (sameEulerTotient(N) == 1)
    Console.Write("Yes");
  else
    Console.Write("No");
}
}
 
// This code is contributed by Rajput-Ji

Java脚本


输出:
Yes

时间复杂度: O(1)
辅助空间: O(1)