📌  相关文章
📜  检查除数是否为偶数或奇数

📅  最后修改于: 2021-04-26 06:44:00             🧑  作者: Mango

给定数字“ n”,发现其除数总数为偶数或奇数。
例子 :

Input  : n = 10      
Output : Even

Input:  n = 100
Output: Odd

Input:  n = 125
Output: Even

我们强烈建议您单击此处并进行实践,然后再继续解决方案。

天真的方法是找到所有除数,然后查看除数的总数是偶数还是奇数。
这种解决方案的时间复杂度为O(sqrt(n))

C++
// Naive Solution to find
// if count of divisors
// is even or odd
#include 
using namespace std;
 
// Function to count
// the divisors
void countDivisors(int n)
{
    // Initialize count
    // of divisors
    int count = 0;
 
    // Note that this
    // loop runs till
    // square root
    for (int i = 1; i <= sqrt(n) + 1; i++)
    {
        if (n % i == 0)
 
            // If divisors are
            // equal increment
            // count by one
            // Otherwise increment
            // count by 2
            count += (n / i == i) ? 1 : 2;
    }
 
    if (count % 2 == 0)
        cout << "Even" << endl;
    else
        cout << "Odd" << endl;
}
 
// Driver Code
int main()
{
    cout << "The count of divisor: ";
    countDivisors(10);
    return 0;
}
 
// This code is Contributed by SHUBHAMSINGH10


C
// Naive Solution to find
// if count of divisors
// is even or odd
 
#include 
#include 
 
// Function to count
// the divisors
void countDivisors(int n)
{
    // Initialize count
    // of divisors
    int count = 0;
 
    // Note that this
    // loop runs till
    // square root
    for (int i = 1; i <= sqrt(n) + 1; i++)
    {
        if (n % i == 0)
 
            // If divisors are
            // equal increment
            // count by one
            // Otherwise increment
            // count by 2
            count += (n / i == i) ? 1 : 2;
    }
 
    if (count % 2 == 0)
        printf("Even\n");
 
    else
        printf("Odd\n");
}
 
// Driver Code
int main()
{
    printf("The count of divisor: ");
    countDivisors(10);
    return 0;
}


Java
// Naive Solution to find if count
// of divisors is even or odd
 
import java.io.*;
import java.math.*;
 
class GFG
{
 
    // Function to count
    // the divisors
    static void countDivisors(int n)
    {
        // Initialize count
        // of divisors
        int count = 0;
 
        // Note that this
        // loop runs till
        // square root
        for (int i = 1; i <= Math.sqrt(n) + 1; i++)
        {
            if (n % i == 0)
 
                // If divisors are
                // equal increment
                // count by one
                // Otherwise increment
                // count by 2
                count += (n / i == i) ? 1 : 2;
        }
 
        if (count % 2 == 0)
            System.out.println("Even");
 
        else
            System.out.println("Odd");
    }
 
    // Driver Code
    public static void main(String args[])
    {
        System.out.print("The count of divisor: ");
        countDivisors(10);
    }
}
// This code is contributed by Nikita Tiwari


Python
# Naive Solution to find if count 
# of divisors is even or odd
import math
 
# Function to count
# the divisors
def countDivisors(n) :
     
    # Initialize count
    # of divisors
    count = 0
 
    # Note that this loop
    # runs till square
    # root
    for i in range(1, (int)(math.sqrt(n)) + 2) :
        if (n % i == 0) :
             
            # If divisors are
            # equal, increment
            # count by one
            # Otherwise increment
            # count by 2
            if( n // i == i) :
                count = count + 1
            else :
                count = count + 2
 
    if (count % 2 == 0) :
        print("Even")
    else :
        print("Odd")
 
 
# Driver Code
print("The count of divisor: ")
countDivisors(10)
 
# This code is contributed by Nikita Tiwari


C#
// C# program using Naive
// Solution to find if
// count of divisors
// is even or odd
using System;
 
class GFG {
 
    // Function to count
    // the divisors
    static void countDivisors(int n)
    {
        // Initialize count
        // of divisors
        int count = 0;
 
        // Note that this
        // loop runs till
        // square root
        for (int i = 1; i <= Math.Sqrt(n)
                                 + 1;
             i++) {
            if (n % i == 0)
 
                // If divisors are
                // equal increment
                // count by one
                // Otherwise increment
                // count by 2
                count += (n / i == i) ? 1 : 2;
        }
 
        if (count % 2 == 0)
            Console.Write("Even");
 
        else
            Console.Write("Odd");
    }
 
    // Driver code
    public static void Main()
    {
        Console.Write("The count of divisor: ");
        countDivisors(10);
    }
}
 
// This code is contributed by Sam007.


PHP


Javascript


C++
// C++ program for
// Efficient Solution to find
// if count of divisors is
// even or odd
#include 
using namespace std;
 
// Function to find if count
// of divisors is even or
// odd
void countDivisors(int n)
{
    int root_n = sqrt(n);
 
    // If n is a perfect square,
    // then it has odd divisors
    if (root_n * root_n == n)
        printf("Odd\n");
    else
        printf("Even\n");
}
 
// Driver Code
int main()
{
    cout << "The count of divisors"
         << " of 10 is: ";
 
    countDivisors(10);
    return 0;
}


Java
// Java program for Efficient
// Solution to find if count of
// divisors is even or odd
import java.io.*;
import java.math.*;
 
class GFG
{
 
    // Function to find if count
    // of divisors is even or
    // odd
    static void countDivisors(int n)
    {
        int root_n = (int)(Math.sqrt(n));
 
        // If n is a perfect square,
        // then, it has odd divisors
        if (root_n * root_n == n)
            System.out.println("Odd");
 
        else
            System.out.println("Even");
    }
 
    // Driver code
    public static void main(String args[])
        throws IOException
    {
        System.out.print("The count of" +
                    "divisors of 10 is: ");
 
        countDivisors(10);
    }
}
 
// This code is contributed by Nikita Tiwari


Python
# Python program for
# Efficient Solution to find
# find if count of divisors
# is even or odd
 
def NumOfDivisor(n):
    if n < 1:
        return
    root_n = n**0.5
     
    # If n is a perfect square,
    # then it has odd divisors
    if root_n**2 == n:
        print('Odd')
    else:
        print('Even')
         
# Driver code    
if __name__ == '__main__':
    print("The count of divisor"+
          "of 10 is: ")
    NumOfDivisor(10)
     
# This code is contributed by Yt R


C#
// C# program for efficient
// solution to find of
// count of divisors is
// even or odd
using System;
 
class GFG {
 
    // Function to find if
    // count of divisors
    // is even or odd
    static void countDivisors(int n)
    {
        int root_n = (int)(Math.Sqrt(n));
 
        // If n is a perfect square,
        // then, it has odd divisors
        if (root_n * root_n == n)
            Console.WriteLine("Odd");
 
        else
            Console.WriteLine("Even");
    }
 
    // Driver code
    public static void Main()
    {
        Console.Write("The count of divisors : ");
 
        countDivisors(10);
    }
}
 
// This code is contributed by Sam007.


PHP


Javascript


输出 :

The count of divisor: Even 

高效的解决方案:
我们可以观察到,除数的奇数仅在理想平方的情况下才是奇数。因此,最好的解决方案是检查给定的数字是否为完美平方。如果它是一个完美的正方形,那么除数的数量将是奇数,否则将是偶数。
下面是上述想法的实现:

C++

// C++ program for
// Efficient Solution to find
// if count of divisors is
// even or odd
#include 
using namespace std;
 
// Function to find if count
// of divisors is even or
// odd
void countDivisors(int n)
{
    int root_n = sqrt(n);
 
    // If n is a perfect square,
    // then it has odd divisors
    if (root_n * root_n == n)
        printf("Odd\n");
    else
        printf("Even\n");
}
 
// Driver Code
int main()
{
    cout << "The count of divisors"
         << " of 10 is: ";
 
    countDivisors(10);
    return 0;
}

Java

// Java program for Efficient
// Solution to find if count of
// divisors is even or odd
import java.io.*;
import java.math.*;
 
class GFG
{
 
    // Function to find if count
    // of divisors is even or
    // odd
    static void countDivisors(int n)
    {
        int root_n = (int)(Math.sqrt(n));
 
        // If n is a perfect square,
        // then, it has odd divisors
        if (root_n * root_n == n)
            System.out.println("Odd");
 
        else
            System.out.println("Even");
    }
 
    // Driver code
    public static void main(String args[])
        throws IOException
    {
        System.out.print("The count of" +
                    "divisors of 10 is: ");
 
        countDivisors(10);
    }
}
 
// This code is contributed by Nikita Tiwari

Python

# Python program for
# Efficient Solution to find
# find if count of divisors
# is even or odd
 
def NumOfDivisor(n):
    if n < 1:
        return
    root_n = n**0.5
     
    # If n is a perfect square,
    # then it has odd divisors
    if root_n**2 == n:
        print('Odd')
    else:
        print('Even')
         
# Driver code    
if __name__ == '__main__':
    print("The count of divisor"+
          "of 10 is: ")
    NumOfDivisor(10)
     
# This code is contributed by Yt R   

C#

// C# program for efficient
// solution to find of
// count of divisors is
// even or odd
using System;
 
class GFG {
 
    // Function to find if
    // count of divisors
    // is even or odd
    static void countDivisors(int n)
    {
        int root_n = (int)(Math.Sqrt(n));
 
        // If n is a perfect square,
        // then, it has odd divisors
        if (root_n * root_n == n)
            Console.WriteLine("Odd");
 
        else
            Console.WriteLine("Even");
    }
 
    // Driver code
    public static void Main()
    {
        Console.Write("The count of divisors : ");
 
        countDivisors(10);
    }
}
 
// This code is contributed by Sam007.

的PHP


Java脚本


输出 :

The count of divisor: Even