Count numbers up to N whose GCD with N is less than that number
Last Updated :
01 Feb, 2023
Given an integer N, the task is to count the values of K ( where 1 ? K? N ), such that 1< GCD(K, N) < K.
Examples:
Input: N = 10
Output: 3
Explanation: The values of K which satisfies the given conditions are:
- K = 4, gcd(4, 10) = 2
- K = 6, gcd(6, 10) = 2
- K = 8, gcd(8, 10) = 2
Input: N = 15
Output: 4
Explanation: The values of K which satisfies the given conditions are:
- K = 6, gcd(6, 15) = 3
- K = 9, gcd(9, 15) = 3
- K = 10, gcd(10, 15) = 5
- K = 12, gcd(12, 15) = 3
Naive Approach: The simplest approach is to iterate over the range [1, N], and check for each number, whether it satisfies the given condition or not. Finally, print the count of such numbers.
Time Complexity: O(N*log(N))
Auxiliary Space: O(1)
Efficient Approach: The idea is to find all the numbers in the range [1, N], for which gcd(K, N) = 1 and gcd(K, N) = K and then finally remove all these numbers from N to get the final answer. Follow the steps below to solve the problem:
- Store all the prime factors of N using Sieve of Eratosthenes.
- Store the count of all the numbers in the range [1, N] for which gcd(N, K) = K in a variable count1.
- Store the count of all the numbers in the range [1, N] for which gcd(N, K) = 1 using Euler’s Totient Function in a variable count2.
- Remove these numbers from N, by updating ans to N – (count1 + count2).
- Print the value of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100001;
vector< int > spf;
void sieve()
{
int p[MAXN + 1] = { 0 };
p[2] = 1;
for ( long long int i = 3; i < MAXN; i += 2) {
p[i] = 1;
}
spf.push_back(2);
for ( long long i = 3; i < MAXN; i += 2) {
if (p[i]) {
spf.push_back(i);
for ( long long int j = i * i; j < MAXN;
j += 2 * i)
p[j] = 0;
}
}
}
void countK( int N)
{
sieve();
int N1 = N;
int div = spf[0];
int index = 1, C = 0;
int count1 = 1;
float count2 = N;
while ( div * div <= N) {
if (N % div == 0) {
C = 0;
while (N % div == 0) {
N /= div ;
C++;
}
count1 = count1 * (C + 1);
count2 *= (1.0 - (1.0 / ( float ) div ));
}
div = spf[index];
index++;
}
if (N != 1) {
count1 *= 2;
count2 = count2 * (1.0 - (1.0 / ( float )N));
}
int ans = N1 - count1 - count2;
ans = ans + 1;
cout << ans;
return ;
}
int main()
{
int N = 10;
countK(N);
return 0;
}
|
Java
import java.util.ArrayList;
class GFG {
private static final int MAXN = 100001 ;
private static ArrayList<Integer> spf
= new ArrayList<>();
private static void sieve()
{
int [] p = new int [MAXN + 1 ];
p[ 2 ] = 1 ;
for ( long i = 3 ; i < MAXN; i += 2 ) {
p[( int )i] = 1 ;
}
spf.add( 2 );
for ( long i = 3 ; i < MAXN; i += 2 ) {
if (p[( int )i] == 1 ) {
spf.add(( int )i);
for ( long j = i * i; j < MAXN; j += 2 * i)
p[( int )j] = 0 ;
}
}
}
private static void countK( int N)
{
sieve();
int N1 = N;
int div = spf.get( 0 );
int index = 1 , C = 0 ;
int count1 = 1 ;
double count2 = N;
while (div * div <= N) {
if (N % div == 0 ) {
C = 0 ;
while (N % div == 0 ) {
N /= div;
C++;
}
count1 = count1 * (C + 1 );
count2 *= ( 1.0 - ( 1.0 / ( double )div));
}
div = spf.get(index);
index++;
}
if (N != 1 ) {
count1 *= 2 ;
count2 = count2 * ( 1.0 - ( 1.0 / ( double )N));
}
int ans = N1 - count1 - ( int )count2;
ans = ans + 1 ;
System.out.println(ans);
return ;
}
public static void main(String[] args)
{
int N = 10 ;
countK(N);
}
}
|
Python3
MAXN = 100001
spf = []
def sieve():
global MAXN
p = [ 0 ] * (MAXN + 1 )
p[ 2 ] = 1
for i in range ( 3 , MAXN, 2 ):
p[i] = 1
spf.append( 2 )
for i in range ( 3 , MAXN, 2 ):
if (p[i]):
spf.append(i)
for j in range (i * i, MAXN, 2 * i):
p[j] = 0
def countK(N):
sieve()
N1 = N
div = spf[ 0 ]
index, C = 1 , 0
count1 = 1
count2 = N
while (div * div < = N):
if (N % div = = 0 ):
C = 0
while (N % div = = 0 ):
N / / = div
C + = 1
count1 = count1 * (C + 1 )
count2 * = ( 1.0 - ( 1.0 / div))
div = spf[index]
index + = 1
if (N ! = 1 ):
count1 * = 2
count2 = count2 * ( 1.0 - ( 1.0 / N))
ans = N1 - count1 - count2
ans = ans + 1
print ( int (ans))
if __name__ = = '__main__' :
N = 10
countK(N)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
private const int MAXN = 100001;
private static List< int > spf = new List< int >();
private static void sieve()
{
int [] p = new int [MAXN + 1];
p[2] = 1;
for ( long i = 3; i < MAXN; i += 2) {
p[i] = 1;
}
spf.Add(2);
for ( long i = 3; i < MAXN; i += 2) {
if (p[i] == 1) {
spf.Add(( int )i);
for ( long j = i * i; j < MAXN; j += 2 * i)
p[j] = 0;
}
}
}
private static void countK( int N)
{
sieve();
int N1 = N;
int div = spf[0];
int index = 1, C = 0;
int count1 = 1;
double count2 = N;
while (div * div <= N) {
if (N % div == 0) {
C = 0;
while (N % div == 0) {
N /= div;
C++;
}
count1 = count1 * (C + 1);
count2 *= (1.0 - (1.0 / ( double )div));
}
div = spf[index];
index++;
}
if (N != 1) {
count1 *= 2;
count2 = count2 * (1.0 - (1.0 / ( double )N));
}
int ans = N1 - count1 - ( int )count2;
ans = ans + 1;
Console.WriteLine(ans);
return ;
}
static void Main( string [] args)
{
int N = 10;
countK(N);
}
}
|
Javascript
<script>
var MAXN = 100001;
var spf = [];
function sieve()
{
var p = Array(MAXN + 1).fill(0);
p[2] = 1;
for ( var i = 3; i < MAXN; i += 2) {
p[i] = 1;
}
spf.push(2);
for ( var i = 3; i < MAXN; i += 2) {
if (p[i]) {
spf.push(i);
for ( var j = i * i; j < MAXN;
j += 2 * i)
p[j] = 0;
}
}
}
function countK(N)
{
sieve();
var N1 = N;
var div = spf[0];
var index = 1, C = 0;
var count1 = 1;
var count2 = N;
while (div * div <= N) {
if (N % div == 0) {
C = 0;
while (N % div == 0) {
N /= div;
C++;
}
count1 = count1 * (C + 1);
count2 *= (1.0 - (1.0 / div));
}
div = spf[index];
index++;
}
if (N != 1) {
count1 *= 2;
count2 = count2 * (1.0 - (1.0 / N));
}
var ans = N1 - count1 - count2;
ans = ans + 1;
document.write(ans);
return ;
}
var N = 10;
countK(N);
</script>
|
Time Complexity: O(N*log(log(N)))
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...