📜  检查一个数字是否具有奇数除数的偶数和偶数除数的偶数

📅  最后修改于: 2021-04-27 19:03:40             🧑  作者: Mango

给定整数N ,任务是检查N是否具有奇数个奇数除数和偶数个偶数除数。

例子

天真的方法:想法是找到数量为N的因子,并对N的奇数因子和N的偶数因子进行计数。最后,检查奇数因子的计数是否为奇数,偶数因子的计数是否为偶数。

下面是上述方法的实现:

C++
// C++ implementation of the
// above approach
 
#include 
using namespace std;
 
#define lli long long int
 
// Function to find the count
// of even and odd factors of N
void checkFactors(lli N)
{
    lli ev_count = 0, od_count = 0;
 
    // Loop runs till square root
    for (lli i = 1;
         i <= sqrt(N) + 1; i++) {
        if (N % i == 0) {
            if (i == N / i) {
                if (i % 2 == 0)
                    ev_count += 1;
                else
                    od_count += 1;
            }
            else {
                if (i % 2 == 0)
                    ev_count += 1;
                else
                    od_count += 1;
                if ((N / i) % 2 == 0)
                    ev_count += 1;
                else
                    od_count += 1;
            }
        }
    }
 
    // Condition to check if the even
    // factors of the number N is
    // is even and count of
    // odd factors is odd
    if (ev_count % 2 == 0
        && od_count % 2 == 1)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}
 
// Driver Code
int main()
{
    lli N = 36;
    checkFactors(N);
    return 0;
}


Java
// Java implementation of the
// above approach
import java.util.*;
 
class GFG{
 
// Function to find the count
// of even and odd factors of N
static void checkFactors(long N)
{
    long ev_count = 0, od_count = 0;
 
    // Loop runs till square root
    for(long i = 1;
             i <= Math.sqrt(N) + 1; i++)
    {
        if (N % i == 0)
        {
            if (i == N / i)
            {
                if (i % 2 == 0)
                    ev_count += 1;
                else
                    od_count += 1;
            }
            else
            {
                if (i % 2 == 0)
                    ev_count += 1;
                else
                    od_count += 1;
                if ((N / i) % 2 == 0)
                    ev_count += 1;
                else
                    od_count += 1;
            }
        }
    }
 
    // Condition to check if the even
    // factors of the number N is
    // is even and count of
    // odd factors is odd
    if (ev_count % 2 == 0 && od_count % 2 == 1)
        System.out.print("Yes" + "\n");
    else
        System.out.print("No" + "\n");
}
 
// Driver Code
public static void main(String[] args)
{
    long N = 36;
     
    checkFactors(N);
}
}
 
// This code is contributed by amal kumar choubey


Python3
# Python3 implementation of the
# above approach
 
# Function to find the count
# of even and odd factors of N
def checkFactors(N):
     
    ev_count = 0; od_count = 0;
 
    # Loop runs till square root
    for i in range(1, int(pow(N, 1 / 2)) + 1):
        if (N % i == 0):
            if (i == N / i):
                 
                if (i % 2 == 0):
                    ev_count += 1;
                else:
                    od_count += 1;
                     
            else:
                if (i % 2 == 0):
                    ev_count += 1;
                else:
                    od_count += 1;
                if ((N / i) % 2 == 0):
                    ev_count += 1;
                else:
                    od_count += 1;
             
    # Condition to check if the even
    # factors of the number N is
    # is even and count of
    # odd factors is odd
    if (ev_count % 2 == 0 and
        od_count % 2 == 1):
        print("Yes" + "");
    else:
        print("No" + "");
 
# Driver Code
if __name__ == '__main__':
     
    N = 36;
 
    checkFactors(N);
 
# This code is contributed by Princi Singh


C#
// C# implementation of the
// above approach
using System;
 
class GFG{
 
// Function to find the count
// of even and odd factors of N
static void checkFactors(long N)
{
    long ev_count = 0, od_count = 0;
 
    // Loop runs till square root
    for(long i = 1;
             i <= Math.Sqrt(N) + 1; i++)
    {
        if (N % i == 0)
        {
            if (i == N / i)
            {
                if (i % 2 == 0)
                    ev_count += 1;
                else
                    od_count += 1;
            }
            else
            {
                if (i % 2 == 0)
                    ev_count += 1;
                else
                    od_count += 1;
                if ((N / i) % 2 == 0)
                    ev_count += 1;
                else
                    od_count += 1;
            }
        }
    }
 
    // Condition to check if the even
    // factors of the number N is
    // is even and count of
    // odd factors is odd
    if (ev_count % 2 == 0 && od_count % 2 == 1)
        Console.Write("Yes" + "\n");
    else
        Console.Write("No" + "\n");
}
 
// Driver Code
public static void Main(String[] args)
{
    long N = 36;
     
    checkFactors(N);
}
}
 
// This code is contributed by Amit Katiyar


Javascript


C++
// C++ implementation of the
// above approach
 
#include 
using namespace std;
 
#define lli long long int
 
// Function to check if the
// number is a perfect square
bool isPerfectSquare(long double x)
{
    long double sr = sqrt(x);
 
    // If square root is an integer
    return ((sr - floor(sr)) == 0);
}
 
// Function to check
// if count of even divisors is even
// and count of odd divisors is odd
void checkFactors(lli N)
{
    if (isPerfectSquare(N))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}
 
// Driver Code
int main()
{
    lli N = 36;
 
    checkFactors(N);
    return 0;
}


Java
// Java implementation of the above approach
class GFG{
     
// Function to check if the
// number is a perfect square
static boolean isPerfectSquare(double x)
{
     
    // Find floating point value of
    // square root of x.
    double sr = Math.sqrt(x);
 
    // If square root is an integer
    return ((sr - Math.floor(sr)) == 0);
}
 
// Function to check if count of
// even divisors is even and count
// of odd divisors is odd
static void checkFactors(int x)
{
    if (isPerfectSquare(x))
        System.out.print("Yes");
    else
        System.out.print("No");
}
 
// Driver code
public static void main(String[] args)
{
    int N = 36;
 
    checkFactors(N);
}
}
 
// This code is contributed by dewantipandeydp


Python3
# Python3 implementation of the above approach
import math
 
# Function to check if the
# number is a perfect square
def isPerfectSquare(x):
 
    # Find floating povalue of
    # square root of x.
    sr = pow(x, 1 / 2);
 
    # If square root is an integer
    return ((sr - math.floor(sr)) == 0);
 
# Function to check if count of
# even divisors is even and count
# of odd divisors is odd
def checkFactors(x):
    if (isPerfectSquare(x)):
        print("Yes");
    else:
        print("No");
 
# Driver code
if __name__ == '__main__':
    N = 36;
 
    checkFactors(N);
 
# This code is contributed by sapnasingh4991


C#
// C# implementation of the above approach
using System;
 
class GFG{
     
// Function to check if the
// number is a perfect square
static bool isPerfectSquare(double x)
{
     
    // Find floating point value of
    // square root of x.
    double sr = Math.Sqrt(x);
 
    // If square root is an integer
    return ((sr - Math.Floor(sr)) == 0);
}
 
// Function to check if count of
// even divisors is even and count
// of odd divisors is odd
static void checkFactors(int x)
{
    if (isPerfectSquare(x))
        Console.Write("Yes");
    else
        Console.Write("No");
}
 
// Driver code
public static void Main(String[] args)
{
    int N = 36;
 
    checkFactors(N);
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
Yes

高效的方法:问题的关键发现是奇数除数的数量是奇数,偶数除数的数量只有在理想平方的情况下才是偶数。因此,最好的解决方案是检查给定的数字是否为完美的平方。如果它是一个理想的正方形,则打印“是”,否则打印“否”。

下面是上述方法的实现:

C++

// C++ implementation of the
// above approach
 
#include 
using namespace std;
 
#define lli long long int
 
// Function to check if the
// number is a perfect square
bool isPerfectSquare(long double x)
{
    long double sr = sqrt(x);
 
    // If square root is an integer
    return ((sr - floor(sr)) == 0);
}
 
// Function to check
// if count of even divisors is even
// and count of odd divisors is odd
void checkFactors(lli N)
{
    if (isPerfectSquare(N))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}
 
// Driver Code
int main()
{
    lli N = 36;
 
    checkFactors(N);
    return 0;
}

Java

// Java implementation of the above approach
class GFG{
     
// Function to check if the
// number is a perfect square
static boolean isPerfectSquare(double x)
{
     
    // Find floating point value of
    // square root of x.
    double sr = Math.sqrt(x);
 
    // If square root is an integer
    return ((sr - Math.floor(sr)) == 0);
}
 
// Function to check if count of
// even divisors is even and count
// of odd divisors is odd
static void checkFactors(int x)
{
    if (isPerfectSquare(x))
        System.out.print("Yes");
    else
        System.out.print("No");
}
 
// Driver code
public static void main(String[] args)
{
    int N = 36;
 
    checkFactors(N);
}
}
 
// This code is contributed by dewantipandeydp

Python3

# Python3 implementation of the above approach
import math
 
# Function to check if the
# number is a perfect square
def isPerfectSquare(x):
 
    # Find floating povalue of
    # square root of x.
    sr = pow(x, 1 / 2);
 
    # If square root is an integer
    return ((sr - math.floor(sr)) == 0);
 
# Function to check if count of
# even divisors is even and count
# of odd divisors is odd
def checkFactors(x):
    if (isPerfectSquare(x)):
        print("Yes");
    else:
        print("No");
 
# Driver code
if __name__ == '__main__':
    N = 36;
 
    checkFactors(N);
 
# This code is contributed by sapnasingh4991

C#

// C# implementation of the above approach
using System;
 
class GFG{
     
// Function to check if the
// number is a perfect square
static bool isPerfectSquare(double x)
{
     
    // Find floating point value of
    // square root of x.
    double sr = Math.Sqrt(x);
 
    // If square root is an integer
    return ((sr - Math.Floor(sr)) == 0);
}
 
// Function to check if count of
// even divisors is even and count
// of odd divisors is odd
static void checkFactors(int x)
{
    if (isPerfectSquare(x))
        Console.Write("Yes");
    else
        Console.Write("No");
}
 
// Driver code
public static void Main(String[] args)
{
    int N = 36;
 
    checkFactors(N);
}
}
 
// This code is contributed by Amit Katiyar

Java脚本


输出:
Yes