Count of numbers up to N having at least one prime factor common with N
Last Updated :
20 Jul, 2022
Given an integer N, the task is to count the number of integers from the range [1, N] having at least one in common prime factor with N other than 1.
Examples:
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
Naive Approach: The simplest approach to solve the given problem is to iterate through all the numbers over the range [1, N], and for every element, if the GCD of each number with N is greater than 1, then increment the count. Otherwise, check for the next number. After checking all the numbers, we print the total count obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countNumbers( int N)
{
int count = 0;
for ( int i = 1; i <= N; i++) {
if (__gcd(i, N) != 1)
count++;
}
cout << count;
}
int main()
{
int N = 5;
countNumbers(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void countNumbers( int N)
{
int count = 0 ;
for ( int i = 1 ; i <= N; i++)
{
if (__gcd(i, N) != 1 )
count++;
}
System.out.print(count);
}
static int __gcd( int a, int b)
{
return b == 0 ? a : __gcd(b, a % b);
}
public static void main(String[] args)
{
int N = 5 ;
countNumbers(N);
}
}
|
Python3
import math
def countNumbers(N):
count = 0
for i in range ( 1 , N + 1 ):
if (math.gcd(i, N) ! = 1 ):
count + = 1
print (count)
if __name__ = = "__main__" :
N = 5
countNumbers(N)
|
C#
using System;
public class GFG{
static void countNumbers( int N)
{
int count = 0;
for ( int i = 1; i <= N; i++)
{
if (__gcd(i, N) != 1)
count++;
}
Console.Write(count);
}
static int __gcd( int a, int b)
{
return b == 0 ? a : __gcd(b, a % b);
}
public static void Main(String[] args)
{
int N = 5;
countNumbers(N);
}
}
|
Javascript
<script>
function countNumbers(N)
{
var count = 0;
for ( var i = 1; i <= N; i++)
{
if (__gcd(i, N) != 1)
count++;
}
document.write(count);
}
function __gcd(a , b)
{
return b == 0 ? a : __gcd(b, a % b);
}
var N = 5;
countNumbers(N);
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1), since no extra space has been taken.
Efficient Approach: The above approach can also be optimized by using Euler’s totient function which gives the count of numbers less than N having no common prime factor with N. Follow the steps below to solve the problem:
- Initialize a variable, say count that stores the numbers having common prime factors other than 1.
- Define a function, phi() to calculate the value of Euler’s totient function which represents the count of integers less than N having no common factor with N.
- After completing the above steps, print the value of (N – phi(N) as the resultant count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int phi( int N)
{
int result = N;
for ( int p = 2; p * p <= N; ++p) {
if (N % p == 0) {
while (N % p == 0)
N /= p;
result -= result / p;
}
}
if (N > 1)
result -= result / N;
return result;
}
int countNumbers( int N)
{
int count = N - phi(N);
cout << count;
}
int main()
{
int N = 5;
countNumbers(N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int phi( int N)
{
int result = N;
for ( int p = 2 ; p * p <= N; ++p)
{
if (N % p == 0 ) {
while (N % p == 0 )
N /= p;
result -= result / p;
}
}
if (N > 1 )
result -= result / N;
return result;
}
static void countNumbers( int N)
{
int count = N - phi(N);
System.out.print(count);
}
public static void main (String[] args)
{
int N = 5 ;
countNumbers(N);
}
}
|
Python3
def phi(N):
result = N
for p in range ( 2 , int ( pow (N, 1 / 2 )) + 1 ):
if (N % p = = 0 ):
while (N % p = = 0 ):
N = N / p
result - = result / / p
if (N > 1 ):
result - = result / / N
return result
def countNumbers(N):
count = N - phi(N)
print (count)
N = 5
countNumbers(N)
|
C#
using System;
public class GFG
{
static int phi( int N)
{
int result = N;
for ( int p = 2; p * p <= N; ++p)
{
if (N % p == 0) {
while (N % p == 0)
N /= p;
result -= result / p;
}
}
if (N > 1)
result -= result / N;
return result;
}
static void countNumbers( int N)
{
int count = N - phi(N);
Console.Write(count);
}
public static void Main(String[] args)
{
int N = 5;
countNumbers(N);
}
}
|
Javascript
<script>
function phi(N)
{
let result = N;
for (let p = 2; p * p <= N; ++p)
{
if (N % p == 0) {
while (N % p == 0)
N /= p;
result -= result / p;
}
}
if (N > 1)
result -= result / N;
return result;
}
function countNumbers(N)
{
let count = N - phi(N);
document.write(count);
}
let N = 5;
countNumbers(N);
</script>
|
Time Complexity: O(sqrt(N))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...