最多为 N 且至少有一个与 N 共有的素数的数的计数
给定一个整数N ,任务是计算范围[1, N]中至少有一个与N不是1的公质因数的整数个数。
例子:
Input: N = 5
Output: 1
Explanation:
Since 5 is prime. Therefore, there is no other number which is at most N, that has at least one common factor with N except N itself. Therefore, the count is 1.
Input: N = 12
Output: 8
朴素方法:解决给定问题的最简单方法是遍历[1, N]范围内的所有数字,并且对于每个元素,如果每个数字的 GCD 与N大于1 ,则增加计数。否则,检查下一个数字。检查所有数字后,我们打印获得的总计数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count all the numbers
// in the range [1, N] having common
// factor with N other than 1
int countNumbers(int N)
{
// Stores the count of numbers
// having more than 1 factor with N
int count = 0;
// Iterate over the range [1, N]
for (int i = 1; i <= N; i++) {
// If gcd is not 1 then
// increment the count
if (__gcd(i, N) != 1)
count++;
}
// Print the resultant count
cout << count;
}
// Driver Code
int main()
{
int N = 5;
countNumbers(N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to count all the numbers
// in the range [1, N] having common
// factor with N other than 1
static void countNumbers(int N)
{
// Stores the count of numbers
// having more than 1 factor with N
int count = 0;
// Iterate over the range [1, N]
for(int i = 1; i <= N; i++)
{
// If gcd is not 1 then
// increment the count
if (__gcd(i, N) != 1)
count++;
}
// Print the resultant count
System.out.print(count);
}
static int __gcd(int a, int b)
{
return b == 0 ? a : __gcd(b, a % b);
}
// Driver Code
public static void main(String[] args)
{
int N = 5;
countNumbers(N);
}
}
// This code is contributed by shikhasingrajput
Python3
# Python 3 program for the above approach
import math
# Function to count all the numbers
# in the range [1, N] having common
# factor with N other than 1
def countNumbers(N):
# Stores the count of numbers
# having more than 1 factor with N
count = 0
# Iterate over the range [1, N]
for i in range(1, N + 1):
# If gcd is not 1 then
# increment the count
if (math.gcd(i, N) != 1):
count += 1
# Print the resultant count
print(count)
# Driver Code
if __name__ == "__main__":
N = 5
countNumbers(N)
# This code is contributed by ukasp.
C#
// C# program for the above approach
using System;
public class GFG{
// Function to count all the numbers
// in the range [1, N] having common
// factor with N other than 1
static void countNumbers(int N)
{
// Stores the count of numbers
// having more than 1 factor with N
int count = 0;
// Iterate over the range [1, N]
for(int i = 1; i <= N; i++)
{
// If gcd is not 1 then
// increment the count
if (__gcd(i, N) != 1)
count++;
}
// Print the resultant count
Console.Write(count);
}
static int __gcd(int a, int b)
{
return b == 0 ? a : __gcd(b, a % b);
}
// Driver Code
public static void Main(String[] args)
{
int N = 5;
countNumbers(N);
}
}
// This code is contributed by 29AjayKumar
Javascript
C++
// C++ program for the above approach
#include
using namespace std;
// Function to calculate the value of
// Euler's totient function
int phi(int N)
{
// Initialize result with N
int result = N;
// Find all prime factors of N
// and subtract their multiples
for (int p = 2; p * p <= N; ++p) {
// Check if p is a prime factor
if (N % p == 0) {
// If found to be true,
// then update N and result
while (N % p == 0)
N /= p;
result -= result / p;
}
}
// If N has a prime factor greater
// than sqrt(N), then there can be
// at-most one such prime factor
if (N > 1)
result -= result / N;
return result;
}
// Function to count all the numbers
// in the range [1, N] having common
// factor with N other than 1
int countNumbers(int N)
{
// Stores the resultant count
int count = N - phi(N);
// Print the count
cout << count;
}
// Driver Code
int main()
{
int N = 5;
countNumbers(N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to calculate the value of
// Euler's totient function
static int phi(int N)
{
// Initialize result with N
int result = N;
// Find all prime factors of N
// and subtract their multiples
for (int p = 2; p * p <= N; ++p)
{
// Check if p is a prime factor
if (N % p == 0) {
// If found to be true,
// then update N and result
while (N % p == 0)
N /= p;
result -= result / p;
}
}
// If N has a prime factor greater
// than sqrt(N), then there can be
// at-most one such prime factor
if (N > 1)
result -= result / N;
return result;
}
// Function to count all the numbers
// in the range [1, N] having common
// factor with N other than 1
static void countNumbers(int N)
{
// Stores the resultant count
int count = N - phi(N);
// Print the count
System.out.print(count);
}
// Driver code
public static void main (String[] args)
{
int N = 5;
countNumbers(N);
}
}
// This code is contributed by offbeat.
Python3
# Python3 program for the above approach
# Function to calculate the value of
# Euler's totient function
def phi(N):
# Initialize result with N
result = N
# Find all prime factors of N
# and subtract their multiples
for p in range(2, int(pow(N, 1 / 2)) + 1):
# Check if p is a prime factor
if (N % p == 0):
# If found to be true,
# then update N and result
while (N % p == 0):
N = N / p
result -= result // p
# If N has a prime factor greater
# than sqrt(N), then there can be
# at-most one such prime factor
if (N > 1):
result -= result // N
return result
# Function to count all the numbers
# in the range [1, N] having common
# factor with N other than 1
def countNumbers(N):
# Stores the resultant count
count = N - phi(N)
# Print the count
print(count)
# Driver Code
N = 5
countNumbers(N)
# This code is contributed by SoumikMondal
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to calculate the value of
// Euler's totient function
static int phi(int N)
{
// Initialize result with N
int result = N;
// Find all prime factors of N
// and subtract their multiples
for (int p = 2; p * p <= N; ++p)
{
// Check if p is a prime factor
if (N % p == 0) {
// If found to be true,
// then update N and result
while (N % p == 0)
N /= p;
result -= result / p;
}
}
// If N has a prime factor greater
// than sqrt(N), then there can be
// at-most one such prime factor
if (N > 1)
result -= result / N;
return result;
}
// Function to count all the numbers
// in the range [1, N] having common
// factor with N other than 1
static void countNumbers(int N)
{
// Stores the resultant count
int count = N - phi(N);
// Print the count
Console.Write(count);
}
// Driver code
public static void Main(String[] args)
{
int N = 5;
countNumbers(N);
}
}
// This code contributed by shikhasingrajput
Javascript
输出:
1
时间复杂度: O(N*log N)
辅助空间: O(1)
有效方法:上述方法也可以通过使用欧拉的 totient函数进行优化,该函数给出小于N且与N没有共同素因数的数字的计数。请按照以下步骤解决问题:
- 初始化一个变量,比如count ,它存储具有除1以外的共同质因数的数字。
- 定义一个函数phi()来计算欧拉函数的值,该函数表示小于N且与N没有公因数的整数的计数。
- 完成上述步骤后,打印(N – phi(N)的值作为结果计数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to calculate the value of
// Euler's totient function
int phi(int N)
{
// Initialize result with N
int result = N;
// Find all prime factors of N
// and subtract their multiples
for (int p = 2; p * p <= N; ++p) {
// Check if p is a prime factor
if (N % p == 0) {
// If found to be true,
// then update N and result
while (N % p == 0)
N /= p;
result -= result / p;
}
}
// If N has a prime factor greater
// than sqrt(N), then there can be
// at-most one such prime factor
if (N > 1)
result -= result / N;
return result;
}
// Function to count all the numbers
// in the range [1, N] having common
// factor with N other than 1
int countNumbers(int N)
{
// Stores the resultant count
int count = N - phi(N);
// Print the count
cout << count;
}
// Driver Code
int main()
{
int N = 5;
countNumbers(N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to calculate the value of
// Euler's totient function
static int phi(int N)
{
// Initialize result with N
int result = N;
// Find all prime factors of N
// and subtract their multiples
for (int p = 2; p * p <= N; ++p)
{
// Check if p is a prime factor
if (N % p == 0) {
// If found to be true,
// then update N and result
while (N % p == 0)
N /= p;
result -= result / p;
}
}
// If N has a prime factor greater
// than sqrt(N), then there can be
// at-most one such prime factor
if (N > 1)
result -= result / N;
return result;
}
// Function to count all the numbers
// in the range [1, N] having common
// factor with N other than 1
static void countNumbers(int N)
{
// Stores the resultant count
int count = N - phi(N);
// Print the count
System.out.print(count);
}
// Driver code
public static void main (String[] args)
{
int N = 5;
countNumbers(N);
}
}
// This code is contributed by offbeat.
Python3
# Python3 program for the above approach
# Function to calculate the value of
# Euler's totient function
def phi(N):
# Initialize result with N
result = N
# Find all prime factors of N
# and subtract their multiples
for p in range(2, int(pow(N, 1 / 2)) + 1):
# Check if p is a prime factor
if (N % p == 0):
# If found to be true,
# then update N and result
while (N % p == 0):
N = N / p
result -= result // p
# If N has a prime factor greater
# than sqrt(N), then there can be
# at-most one such prime factor
if (N > 1):
result -= result // N
return result
# Function to count all the numbers
# in the range [1, N] having common
# factor with N other than 1
def countNumbers(N):
# Stores the resultant count
count = N - phi(N)
# Print the count
print(count)
# Driver Code
N = 5
countNumbers(N)
# This code is contributed by SoumikMondal
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to calculate the value of
// Euler's totient function
static int phi(int N)
{
// Initialize result with N
int result = N;
// Find all prime factors of N
// and subtract their multiples
for (int p = 2; p * p <= N; ++p)
{
// Check if p is a prime factor
if (N % p == 0) {
// If found to be true,
// then update N and result
while (N % p == 0)
N /= p;
result -= result / p;
}
}
// If N has a prime factor greater
// than sqrt(N), then there can be
// at-most one such prime factor
if (N > 1)
result -= result / N;
return result;
}
// Function to count all the numbers
// in the range [1, N] having common
// factor with N other than 1
static void countNumbers(int N)
{
// Stores the resultant count
int count = N - phi(N);
// Print the count
Console.Write(count);
}
// Driver code
public static void Main(String[] args)
{
int N = 5;
countNumbers(N);
}
}
// This code contributed by shikhasingrajput
Javascript
输出:
1
时间复杂度: O(N 1/2 )
辅助空间: O(1)