📜  检查从 1 到 N 的因子总数的总和是偶数还是奇数

📅  最后修改于: 2022-05-13 01:56:09.437000             🧑  作者: Mango

检查从 1 到 N 的因子总数的总和是偶数还是奇数

给定一个整数N 。任务是检查从1N的因子总数的总和是偶数还是奇数,即sum % 2 = 0 或 1

例子:

朴素的方法:解决问题的基本思路是首先找到从1N的数字的所有因数,然后存储它们的和并检查和是偶数还是奇数。

下面是上述方法的实现。

C++
// C++ code for the above approach:
 
#include 
using namespace std;
 
// Function to find the factors
int factor(int n)
{
    int count = 0;
 
    // Counting the factors for
    // all the numbers.
    for (int i = 1; i <= n; i++) {
 
        // Counting the factors
        // for number i
        for (int j = 1; j < sqrt(n) + 1;
             j++) {
            if (i % j == 0) {
                if (i / j == j) {
                    count += 1;
                }
                else {
                    count += 2;
                }
                count %= 2;
            }
        }
    }
 
    // Return the count
    return count;
}
 
// Driver Code
int main()
{
    int N = 4;
 
    // Function call
    if (factor(N))
        cout << "Odd";
    else
        cout << "Even";
    return 0;
}


Java
// Java code for the above approach:
import java.io.*;
class GFG {
 
  // Function to find the factors
  static int factor(int n)
  {
    int count = 0;
 
    // Counting the factors for
    // all the numbers.
    for (int i = 1; i <= n; i++) {
 
      // Counting the factors
      // for number i
      for (int j = 1; j < Math.sqrt(n) + 1; j++) {
        if (i % j == 0) {
          if (i / j == j) {
            count += 1;
          }
          else {
            count += 2;
          }
          count %= 2;
        }
      }
    }
 
    // Return the count
    return count;
  }
 
  // Driver Code
  public static void main (String[] args) {
    int N = 4;
 
    // Function call
    if (factor(N) != 0)
      System.out.print("Odd");
    else
      System.out.print("Even");
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# Python code for the above approach:
 
# Function to find the factors
import math
def factor(n):
 
   count = 0
 
    # Counting the factors for
    # all the numbers.
   for i in range(1, n + 1):
 
        # Counting the factors
        # for number i
        for j in range(1,math.floor(math.sqrt(n)+1)):
            if (i % j == 0):
               if (i / j == j):
                  count += 1
               else:
                  count += 2
               count %= 2
 
    # Return the count
   return count
 
# Driver Code
N = 4
 
# Function call
if(factor(N)):
   print("Odd")
else:
   print("Even")
 
# This code is contributed by shinjanpatra


C#
// C# code for the above approach:
using System;
class GFG {
 
  // Function to find the factors
  static int factor(int n)
  {
    int count = 0;
 
    // Counting the factors for
    // all the numbers.
    for (int i = 1; i <= n; i++) {
 
      // Counting the factors
      // for number i
      for (int j = 1; j < Math.Sqrt(n) + 1; j++) {
        if (i % j == 0) {
          if (i / j == j) {
            count += 1;
          }
          else {
            count += 2;
          }
          count %= 2;
        }
      }
    }
 
    // Return the count
    return count;
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 4;
 
    // Function call
    if (factor(N) != 0)
      Console.Write("Odd");
    else
      Console.Write("Even");
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


C++
// C++ code for the above approach:
 
#include 
using namespace std;
 
// Function to find the count of factors
int factor(int N)
{
    int count = 0;
 
    // Finding out the number of
    // perfect squares that lie
    // between 1 to N.
    count = (int)sqrt(N);
    count = count % 2;
    return count;
}
 
// Driver code
int main()
{
    int N = 4;
 
    // Function call
    if (factor(N))
        cout << "Odd";
    else
        cout << "Even";
    return 0;
}


Java
// JAVA code for the above approach:
import java.util.*;
class GFG
{
 
  // Function to find the count of factors
  public static int factor(int N)
  {
    int count = 0;
 
    // Finding out the number of
    // perfect squares that lie
    // between 1 to N.
    count = (int)Math.sqrt(N);
    count = count % 2;
    return count;
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int N = 4;
 
    // Function call
    if (factor(N) != 0)
      System.out.print("Odd");
    else
      System.out.print("Even");
  }
}
 
// This code is contributed by Taranpreet


Python3
# Python code for the above approach:
 
# Function to find the count of factors
import math
 
def factor(N):
 
    count = 0
 
    # Finding out the number of
    # perfect squares that lie
    # between 1 to N.
    count = math.floor(math.sqrt(N))
    count = count % 2
    return count
 
# Driver code
 
N = 4
 
# Function call
if (factor(N)):
    print("Odd")
else:
    print("Even")
     
    # This code is contributed by shinjanpatra.


C#
// C# code for the above approach:
using System;
class GFG {
 
    // Function to find the count of factors
    static int factor(int N)
    {
        int count = 0;
 
        // Finding out the number of
        // perfect squares that lie
        // between 1 to N.
        count = (int)Math.Sqrt(N);
        count = count % 2;
        return count;
    }
 
    // Driver code
    public static void Main()
    {
        int N = 4;
 
        // Function call
        if (factor(N) != 0)
            Console.Write("Odd");
        else
            Console.Write("Even");
    }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
Even

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

有效方法:有效解决问题的想法基于以下观察。

请按照以下步骤解决问题:

  • 声明任何计数变量来存储一个数的因数,并将其初始化为 0。
  • 使用从1 到 N的完美正方形总数更新计数,这与 sqrt(N) 相同。
  • 检查count是偶数还是奇数,并相应地返回 0 或 1。

下面是上述方法的实现:

C++

// C++ code for the above approach:
 
#include 
using namespace std;
 
// Function to find the count of factors
int factor(int N)
{
    int count = 0;
 
    // Finding out the number of
    // perfect squares that lie
    // between 1 to N.
    count = (int)sqrt(N);
    count = count % 2;
    return count;
}
 
// Driver code
int main()
{
    int N = 4;
 
    // Function call
    if (factor(N))
        cout << "Odd";
    else
        cout << "Even";
    return 0;
}

Java

// JAVA code for the above approach:
import java.util.*;
class GFG
{
 
  // Function to find the count of factors
  public static int factor(int N)
  {
    int count = 0;
 
    // Finding out the number of
    // perfect squares that lie
    // between 1 to N.
    count = (int)Math.sqrt(N);
    count = count % 2;
    return count;
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int N = 4;
 
    // Function call
    if (factor(N) != 0)
      System.out.print("Odd");
    else
      System.out.print("Even");
  }
}
 
// This code is contributed by Taranpreet

Python3

# Python code for the above approach:
 
# Function to find the count of factors
import math
 
def factor(N):
 
    count = 0
 
    # Finding out the number of
    # perfect squares that lie
    # between 1 to N.
    count = math.floor(math.sqrt(N))
    count = count % 2
    return count
 
# Driver code
 
N = 4
 
# Function call
if (factor(N)):
    print("Odd")
else:
    print("Even")
     
    # This code is contributed by shinjanpatra.

C#

// C# code for the above approach:
using System;
class GFG {
 
    // Function to find the count of factors
    static int factor(int N)
    {
        int count = 0;
 
        // Finding out the number of
        // perfect squares that lie
        // between 1 to N.
        count = (int)Math.Sqrt(N);
        count = count % 2;
        return count;
    }
 
    // Driver code
    public static void Main()
    {
        int N = 4;
 
        // Function call
        if (factor(N) != 0)
            Console.Write("Odd");
        else
            Console.Write("Even");
    }
}
 
// This code is contributed by Samim Hossain Mondal.

Javascript



输出
Even

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