检查从 1 到 N 的因子总数的总和是偶数还是奇数
给定一个整数N 。任务是检查从1到N的因子总数的总和是偶数还是奇数,即sum % 2 = 0 或 1 。
例子:
Input: N = 3
Output: Odd
Explanation: 1 has 1 factor, 2 has 2 factors (1, 2) and 3 has 2 factors (1, 3),
Hence, total number of factors is 5 which is odd.
Input: N = 4
Output: Even
Explanation: 1 has 1 factor, 2 has 2 factors (1, 2), 3 has 2 factors (1, 3) and 4 has 3 factors (1, 2, 4),
Hence, a total of 8 factors which is even.
朴素的方法:解决问题的基本思路是首先找到从1到N的数字的所有因数,然后存储它们的和并检查和是偶数还是奇数。
下面是上述方法的实现。
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)
有效方法:有效解决问题的想法基于以下观察。
As, all numbers except perfect squares, have an even number of factors. So, the concern is only perfect squares.
Find the number of perfect squares that lie between 1 to N.
If the number of perfect squares is odd then total number of factors is odd, otherwise, even.
请按照以下步骤解决问题:
- 声明任何计数变量来存储一个数的因数,并将其初始化为 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)