📜  检查N的偶数除数是否等于奇数除数

📅  最后修改于: 2021-04-29 11:46:53             🧑  作者: Mango

给定一个正整数N,任务是检查甚至除数和N个奇约数的计数是否相等。如果它们相同,则打印“是”,否则打印“否”
例子 :

幼稚的方法:幼稚的方法是找到给定数目的所有除数,并计算偶数除数和奇数除数,并检查它们是否相等。如果它们相同,则打印“是” ,否则打印“否”
下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if count of even
// and odd divisors are equal
bool divisorsSame(int n)
{
    // To store the count of even
    // factors and odd factors
    int even_div = 0, odd_div = 0;
 
    // Loop till [1, sqrt(N)]
    for (int i = 1; i <= sqrt(n); i++) {
        if (n % i == 0) {
 
            // If divisors are equal
            // add only one
            if (n / i == i) {
 
                // Check for even
                // divisor
                if (i % 2 == 0) {
                    even_div++;
                }
 
                // Odd divisor
                else {
                    odd_div++;
                }
            }
 
            // Check for both divisor
            // i.e., i and N/i
            else {
 
                // Check if i is odd
                // or even
                if (i % 2 == 0) {
                    even_div++;
                }
                else {
                    odd_div++;
                }
 
                // Check if N/i is odd
                // or even
                if (n / i % 2 == 0) {
                    even_div++;
                }
                else {
                    odd_div++;
                }
            }
        }
    }
 
    // Return true if count of even_div
    // and odd_div are equals
    return (even_div == odd_div);
}
 
// Driver Code
int main()
{
    // Given Number
    int N = 6;
 
    // Function Call
    if (divisorsSame(N)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}


Java
// Java code for the above program
import java.util.*;
 
class GFG{
 
// Function to check if count of
// even and odd divisors are equal
static boolean divisorsSame(int n)
{
     
    // To store the count of even
    // factors and odd factors
    int even_div = 0, odd_div = 0;
 
    // Loop till [1, sqrt(N)]
    for(int i = 1; i <= Math.sqrt(n); i++)
    {
       if (n % i == 0)
       {
            
           // If divisors are equal
           // add only one
           if (n / i == i)
           {
                
               // Check for even
               // divisor
               if (i % 2 == 0)
               {
                   even_div++;
               }
                
               // Odd divisor
               else
               {
                   odd_div++;
               }
           }
            
           // Check for both divisor
           // i.e., i and N/i
           else
           {
                
               // Check if i is odd
               // or even
               if (i % 2 == 0)
               {
                   even_div++;
               }
               else
               {
                   odd_div++;
               }
                
               // Check if N/i is odd
               // or even
               if (n / i % 2 == 0)
               {
                   even_div++;
               }
               else
               {
                   odd_div++;
               }
           }
       }
    }
     
    // Return true if count of even_div
    // and odd_div are equals
    return (even_div == odd_div);
}
 
// Driver code
public static void main(String[] args)
{
    // Given number
    int N = 6;
 
    // Function call
    if (divisorsSame(N))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program for the above approach
import math
 
# Function to check if count of even
# and odd divisors are equal
def divisorsSame(n):
 
    # To store the count of even
    # factors and odd factors
    even_div = 0; odd_div = 0;
 
    # Loop till [1, sqrt(N)]
    for i in range(1, int(math.sqrt(n))):
     
        if (n % i == 0):
 
            # If divisors are equal
            # add only one
            if (n // i == i):
 
                # Check for even
                # divisor
                if (i % 2 == 0):
                    even_div += 1;
                 
                # Odd divisor
                else:
                    odd_div += 1;
 
            # Check for both divisor
            # i.e., i and N/i
            else:
 
                # Check if i is odd
                # or even
                if (i % 2 == 0):
                    even_div += 1;
                 
                else:
                    odd_div += 1;
                 
                # Check if N/i is odd
                # or even
                if (n // (i % 2) == 0):
                    even_div += 1;
                 
                else:
                    odd_div += 1;
                 
    # Return true if count of even_div
    # and odd_div are equals
    return (even_div == odd_div);
 
# Driver Code
 
# Given Number
N = 6;
 
# Function Call
if (divisorsSame(N) == 0):
    print("Yes");
 
else:
    print("No");
 
# This code is contributed by Code_Mech


C#
// C# code for the above program
using System;
class GFG{
 
// Function to check if count of
// even and odd divisors are equal
static bool divisorsSame(int n)
{
     
    // To store the count of even
    // factors and odd factors
    int even_div = 0, odd_div = 0;
 
    // Loop till [1, sqrt(N)]
    for(int i = 1; i <= Math.Sqrt(n); i++)
    {
        if (n % i == 0)
        {
                 
            // If divisors are equal
            // add only one
            if (n / i == i)
            {
                     
                // Check for even
                // divisor
                if (i % 2 == 0)
                {
                    even_div++;
                }
                     
                // Odd divisor
                else
                {
                    odd_div++;
                }
            }
                 
            // Check for both divisor
            // i.e., i and N/i
            else
            {
                     
                // Check if i is odd
                // or even
                if (i % 2 == 0)
                {
                    even_div++;
                }
                else
                {
                    odd_div++;
                }
                     
                // Check if N/i is odd
                // or even
                if (n / i % 2 == 0)
                {
                    even_div++;
                }
                else
                {
                    odd_div++;
                }
            }
        }
    }
     
    // Return true if count of even_div
    // and odd_div are equals
    return (even_div == odd_div);
}
 
// Driver code
public static void Main()
{
    // Given number
    int N = 6;
 
    // Function call
    if (divisorsSame(N))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
 
// This code is contributed by Akanksha_Rai


Javascript


C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if count of even
// and odd divisors are equal
bool divisorsSame(int n)
{
 
    // If (n-2)%4 is an integer, then
    // return true else return false
    return (n - 2) % 4 == 0;
}
 
// Driver Code
int main()
{
    // Given Number
    int N = 6;
 
    // Function Call
    if (divisorsSame(N)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}


Java
// Java code for the above program
import java.util.*;
 
class GFG{
     
// Function to check if count of
// even and odd divisors are equal
static boolean divisorsSame(int n)
{
     
    // If (n-2)%4 is an integer, then
    // return true else return false
    return (n - 2) % 4 == 0;
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given number
    int N = 6;
 
    // Function call
    if (divisorsSame(N))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
    }
}
 
// This code is contributed by offbeat


Python3
# Python3 program for the above approach
 
# Function to check if count of even
# and odd divisors are equal
def divisorsSame(n):
 
    # If (n-2)%4 is an integer, then
    # return true else return false
    return (n - 2) % 4 == 0;
 
# Driver Code
 
# Given Number
N = 6;
 
# Function Call
if (divisorsSame(N)):
    print("Yes");
else:
    print("No");
 
# This code is contributed by Nidhi_biet


C#
// C# code for the above program
using System;
class GFG{
     
// Function to check if count of
// even and odd divisors are equal
static bool divisorsSame(int n)
{
     
    // If (n-2)%4 is an integer, then
    // return true else return false
    return (n - 2) % 4 == 0;
}
 
// Driver code
public static void Main()
{
     
    // Given number
    int N = 6;
 
    // Function call
    if (divisorsSame(N))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
    }
}
 
// This code is contributed by Code_Mech


Javascript


输出:
Yes

时间复杂度: O(√N) ,其中N是给定的数字
辅助空间: O(1)
有效方法:想法是观察偶数和奇数除数等于的数形成以下算术级数:

  1. 上述系列的第K个术语是:
    K^{th} Term = 4*K + 2
  2. 现在,我们必须通过以下公式检查N是否为上述系列的一项:
  1. 如果使用上述公式计算的K值为整数,则N为偶数和奇数除数相等的数字。
  2. 其他N并不是偶数和奇数除数相等的数字。

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if count of even
// and odd divisors are equal
bool divisorsSame(int n)
{
 
    // If (n-2)%4 is an integer, then
    // return true else return false
    return (n - 2) % 4 == 0;
}
 
// Driver Code
int main()
{
    // Given Number
    int N = 6;
 
    // Function Call
    if (divisorsSame(N)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}

Java

// Java code for the above program
import java.util.*;
 
class GFG{
     
// Function to check if count of
// even and odd divisors are equal
static boolean divisorsSame(int n)
{
     
    // If (n-2)%4 is an integer, then
    // return true else return false
    return (n - 2) % 4 == 0;
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given number
    int N = 6;
 
    // Function call
    if (divisorsSame(N))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
    }
}
 
// This code is contributed by offbeat

Python3

# Python3 program for the above approach
 
# Function to check if count of even
# and odd divisors are equal
def divisorsSame(n):
 
    # If (n-2)%4 is an integer, then
    # return true else return false
    return (n - 2) % 4 == 0;
 
# Driver Code
 
# Given Number
N = 6;
 
# Function Call
if (divisorsSame(N)):
    print("Yes");
else:
    print("No");
 
# This code is contributed by Nidhi_biet

C#

// C# code for the above program
using System;
class GFG{
     
// Function to check if count of
// even and odd divisors are equal
static bool divisorsSame(int n)
{
     
    // If (n-2)%4 is an integer, then
    // return true else return false
    return (n - 2) % 4 == 0;
}
 
// Driver code
public static void Main()
{
     
    // Given number
    int N = 6;
 
    // Function call
    if (divisorsSame(N))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
    }
}
 
// This code is contributed by Code_Mech

Java脚本


输出:
Yes

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