给定一个正整数N ,任务是找到正整数的数量,其 GCD 与给定的整数N是数字本身。
例子:
Input: N = 5
Output: 2
Explanation:
Following are the numbers whose GCD with N is the number itself:
- Number 1: GCD(1, 5) = 1.
- Number 1: GCD(5, 5) = 5.
Therefore, the total count is 2.
Input: N = 10
Output: 4
方法:给定的问题可以基于以下观察来解决:任何数字(比如K )的 GCD 与N的必要条件是K当且仅当K是N 的一个因子。因此,想法是找到N的因子数。请按照以下步骤解决问题:
- 初始化一个变量,比如count为0 ,以计算N的因子数。
- 迭代范围[1, sqrt(N)]并执行以下步骤:
- 如果当前数字i除以给定的整数N ,则将count增加1 。
- 如果i和N / i的值不同,则将count增加1 。
- 完成以上步骤后,打印count的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count numbers whose
// GCD with N is the number itself
int countNumbers(int N)
{
// Stores the count of factors of N
int count = 0;
// Iterate over the range [1, sqrt(N)]
for (int i = 1; i * i <= N; i++) {
// If i is divisible by i
if (N % i == 0) {
// Increment count
count++;
// Avoid counting the
// same factor twice
if (N / i != i) {
count++;
}
}
}
// Return the resultant count
return count;
}
// Driver Code
int main()
{
int N = 10;
cout << countNumbers(N);
return 0;
}
Java
// java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG {
// Function to count numbers whose
// GCD with N is the number itself
static int countNumbers(int N)
{
// Stores the count of factors of N
int count = 0;
// Iterate over the range [1, sqrt(N)]
for (int i = 1; i * i <= N; i++) {
// If i is divisible by i
if (N % i == 0) {
// Increment count
count++;
// Avoid counting the
// same factor twice
if (N / i != i) {
count++;
}
}
}
// Return the resultant count
return count;
}
// Driver Code
public static void main(String[] args)
{
int N = 10;
System.out.println(countNumbers(N));
}
}
// This code is contributed by Kingash.
Python3
# Python3 program for the above approach
# Function to count numbers whose
# GCD with N is the number itself
def countNumbers(N):
# Stores the count of factors of N
count = 0
# Iterate over the range [1, sqrt(N)]
for i in range(1, N + 1):
if i * i > N:
break
# If i is divisible by i
if (N % i == 0):
# Increment count
count += 1
# Avoid counting the
# same factor twice
if (N // i != i):
count += 1
# Return the resultant count
return count
# Driver Code
if __name__ == '__main__':
N = 10
print(countNumbers(N))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
public class GFG {
// Function to count numbers whose
// GCD with N is the number itself
static int countNumbers(int N)
{
// Stores the count of factors of N
int count = 0;
// Iterate over the range [1, sqrt(N)]
for (int i = 1; i * i <= N; i++) {
// If i is divisible by i
if (N % i == 0) {
// Increment count
count++;
// Avoid counting the
// same factor twice
if (N / i != i) {
count++;
}
}
}
// Return the resultant count
return count;
}
// Driver Code
public static void Main(string[] args)
{
int N = 10;
Console.WriteLine(countNumbers(N));
}
}
// This code is contributed by ukasp.
Javascript
输出:
4
时间复杂度: O(N 1/2 )
辅助空间: O(1)