Count of sets possible using integers from a range [2, N] using given operations that are in Equivalence Relation
Last Updated :
15 Dec, 2021
Given an integer N, repeatedly choose two distinct integers from the range 2 to N and if their GCD is found to be greater than 1, insert them into the same set, as long as possible. The sets formed in Equivalence Relation. Therefore, if integers a and b are in the same set and integers b and c are in the same set, then integers a and c are also said to be in the same group. The task is to find the total number of such sets that can be formed.
Examples:
Input: N = 3
Output: 2
Explanation: Sets formed are: {2}, {3}. They cannot be put in the same set because there GCD is 1.
Input: N = 9
Output : 3
Sets formed are : {2, 3, 4, 6, 8, 9}, {5}, {7}
As {2, 4, 6, 8} lies in same set and {3, 6, 9} also lies in same set. Hence, all these lie in one set together, because 6 is the common element in both the sets.
Approach: The idea to solve the problem is based on the following observation that all the numbers less than or equal to N/2 belong to the same set because if 2 is multiplied in them, they will be even number and has GCD greater than 1 with 2. So the remaining sets are formed by numbers greater than N/2 and are prime because if they are not prime then there is a number less than or equal to N/2 which is the divisor of that number. The prime numbers from 2 to N can be found using the Sieve of Eratosthenes.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool prime[100001];
void SieveOfEratosthenes( int n)
{
memset (prime, true , sizeof (prime));
for ( int p = 2; p * p <= n; p++) {
if (prime[p] == true ) {
for ( int i = p * p; i <= n; i += p)
prime[i] = false ;
}
}
}
void NumberofSets( int N)
{
SieveOfEratosthenes(N);
if (N == 2) {
cout << 1 << endl;
}
else if (N == 3) {
cout << 2 << endl;
}
else {
int ans = 1;
for ( int i = N / 2 + 1; i <= N; i++) {
if (prime[i]) {
ans += 1;
}
}
cout << ans << endl;
}
}
int main()
{
int N = 9;
NumberofSets(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean prime[] = new boolean [ 100001 ];
static void SieveOfEratosthenes( int n)
{
Arrays.fill(prime, true );
for ( int p = 2 ; p * p <= n; p++)
{
if (prime[p] == true )
{
for ( int i = p * p; i <= n; i += p)
prime[i] = false ;
}
}
}
static void NumberofSets( int N)
{
SieveOfEratosthenes(N);
if (N == 2 )
{
System.out.print( 1 );
}
else if (N == 3 )
{
System.out.print( 2 );
}
else
{
int ans = 1 ;
for ( int i = N / 2 + 1 ; i <= N; i++)
{
if (prime[i])
{
ans += 1 ;
}
}
System.out.print(ans);
}
}
public static void main(String[] args)
{
int N = 9 ;
NumberofSets(N);
}
}
|
Python3
prime = [ True ] * 100001
def SieveOfEratosthenes(n):
global prime
for p in range ( 2 , n + 1 ):
if p * p > n:
break
if (prime[p] = = True ):
for i in range (p * p, n + 1 , p):
prime[i] = False
def NumberofSets(N):
SieveOfEratosthenes(N)
if (N = = 2 ):
print ( 1 )
elif (N = = 3 ):
print ( 2 )
else :
ans = 1
for i in range (N / / 2 , N + 1 ):
if (prime[i]):
ans + = 1
print (ans)
if __name__ = = '__main__' :
N = 9
NumberofSets(N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static bool []prime = new bool [100001];
static void SieveOfEratosthenes( int n)
{
for ( int i=0;i<100001;i++)
prime[i] = true ;
for ( int p = 2; p * p <= n; p++) {
if (prime[p] == true ) {
for ( int i = p * p; i <= n; i += p)
prime[i] = false ;
}
}
}
static void NumberofSets( int N)
{
SieveOfEratosthenes(N);
if (N == 2) {
Console.Write(1);
}
else if (N == 3) {
Console.Write(2);
}
else {
int ans = 1;
for ( int i = N / 2 + 1; i <= N; i++) {
if (prime[i]) {
ans += 1;
}
}
Console.Write(ans);
}
}
public static void Main()
{
int N = 9;
NumberofSets(N);
}
}
|
Javascript
<script>
let prime = new Array(100001);
function SieveOfEratosthenes(n)
{
for (let i=0;i<prime.length;i++)
{
prime[i]= true ;
}
for (let p = 2; p * p <= n; p++)
{
if (prime[p] == true )
{
for (let i = p * p; i <= n; i += p)
prime[i] = false ;
}
}
}
function NumberofSets(N)
{
SieveOfEratosthenes(N);
if (N == 2)
{
document.write(1);
}
else if (N == 3)
{
document.write(2);
}
else
{
let ans = 1;
for (let i = Math.floor(N / 2) + 1; i <= N; i++)
{
if (prime[i])
{
ans += 1;
}
}
document.write(ans);
}
}
let N = 9;
NumberofSets(N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(K)
Share your thoughts in the comments
Please Login to comment...