 GFG App
Open App Browser
Continue

# Count of sets possible using integers from a range [2, N] using given operations that are in Equivalence Relation

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++

 `// C++ program for the above approach` `#include ` `using` `namespace` `std;` `bool` `prime;`   `// Sieve of Eratosthenes to find` `// primes less than or equal to N` `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``;` `        ``}` `    ``}` `}`   `// Function to find number of Sets` `void` `NumberofSets(``int` `N)` `{` `    ``SieveOfEratosthenes(N);`   `    ``// Handle Base Case` `    ``if` `(N == 2) {` `        ``cout << 1 << endl;` `    ``}` `    ``else` `if` `(N == 3) {` `        ``cout << 2 << endl;` `    ``}` `    ``else` `{`   `        ``// Set which contains less` `        ``// than or equal to N/2` `        ``int` `ans = 1;`   `        ``// Number greater than N/2 and` `        ``// are prime increment it by 1` `        ``for` `(``int` `i = N / 2 + 1; i <= N; i++) {`   `            ``// If the number is prime` `            ``// Increment answer by 1` `            ``if` `(prime[i]) {` `                ``ans += 1;` `            ``}` `        ``}`   `        ``cout << ans << endl;` `    ``}` `}` `// Driver Code` `int` `main()` `{` `    ``// Input` `    ``int` `N = 9;`   `    ``// Function Call` `    ``NumberofSets(N);`   `    ``return` `0;` `}`

## Java

 `// Java program for the above approach` `import` `java.util.*;`   `class` `GFG{` `    `  `static` `boolean` `prime[] = ``new` `boolean``[``100001``];`   `// Sieve of Eratosthenes to find` `// primes less than or equal to N` `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``;` `        ``}` `    ``}` `}`   `// Function to find number of Sets` `static` `void` `NumberofSets(``int` `N)` `{` `    ``SieveOfEratosthenes(N);`   `    ``// Handle Base Case` `    ``if` `(N == ``2``) ` `    ``{` `        ``System.out.print(``1``);` `    ``}` `    ``else` `if` `(N == ``3``)` `    ``{` `        ``System.out.print(``2``);` `    ``}` `    ``else` `    ``{` `        `  `        ``// Set which contains less` `        ``// than or equal to N/2` `        ``int` `ans = ``1``;`   `        ``// Number greater than N/2 and` `        ``// are prime increment it by 1` `        ``for``(``int` `i = N / ``2` `+ ``1``; i <= N; i++)` `        ``{` `            `  `            ``// If the number is prime` `            ``// Increment answer by 1` `            ``if` `(prime[i]) ` `            ``{` `                ``ans += ``1``;` `            ``}` `        ``}` `        ``System.out.print(ans);` `    ``}` `}`   `// Driver Code` `public` `static` `void` `main(String[] args) ` `{` `    `  `    ``// Input` `    ``int` `N = ``9``;`   `    ``// Function Call` `    ``NumberofSets(N);` `}    ` `}`   `// This code is contributed by code_hunt`

## Python3

 `# Python3 program for the above approach` `prime ``=` `[``True``] ``*` `100001`   `# Sieve of Eratosthenes to find` `# primes less than or equal to N` `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`   `# Function to find number of Sets` `def` `NumberofSets(N):` `    `  `    ``SieveOfEratosthenes(N)`   `    ``# Handle Base Case` `    ``if` `(N ``=``=` `2``):` `        ``print``(``1``)` `    ``elif` `(N ``=``=` `3``):` `        ``print``(``2``)` `    ``else``:` `        `  `        ``# Set which contains less` `        ``# than or equal to N/2` `        ``ans ``=` `1`   `        ``# Number greater than N/2 and` `        ``# are prime increment it by 1` `        ``for` `i ``in` `range``(N ``/``/` `2``, N ``+` `1``):` `            `  `            ``# If the number is prime` `            ``# Increment answer by 1` `            ``if` `(prime[i]):` `                ``ans ``+``=` `1`   `        ``print``(ans)`   `# Driver Code` `if` `__name__ ``=``=` `'__main__'``:` `    `  `    ``# Input` `    ``N ``=` `9`   `    ``# Function Call` `    ``NumberofSets(N)`   `# This code is contributed by mohit kumar 29`

## C#

 `// C# program for the above approach` `using` `System;` `using` `System.Collections.Generic;`   `class` `GFG{` `static` `bool` `[]prime = ``new` `bool``;`   `// Sieve of Eratosthenes to find` `// primes less than or equal to N` `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``;` `        ``}` `    ``}` `}`   `// Function to find number of Sets` `static` `void` `NumberofSets(``int` `N)` `{` `    ``SieveOfEratosthenes(N);`   `    ``// Handle Base Case` `    ``if` `(N == 2) {` `        ``Console.Write(1);` `    ``}` `    ``else` `if` `(N == 3) {` `        ``Console.Write(2);` `    ``}` `    ``else` `{`   `        ``// Set which contains less` `        ``// than or equal to N/2` `        ``int` `ans = 1;`   `        ``// Number greater than N/2 and` `        ``// are prime increment it by 1` `        ``for` `(``int` `i = N / 2 + 1; i <= N; i++) {`   `            ``// If the number is prime` `            ``// Increment answer by 1` `            ``if` `(prime[i]) {` `                ``ans += 1;` `            ``}` `        ``}`   `        ``Console.Write(ans);` `    ``}` `}` `  `  `// Driver Code` `public` `static` `void` `Main()` `{` `    ``// Input` `    ``int` `N = 9;`   `    ``// Function Call` `    ``NumberofSets(N);` `}` `}`   `// This code is contributed by SURENDRA_GANGWAR.`

## Javascript

 ``

Output:

`3`

Time Complexity: O(N)
Auxiliary Space: O(K)

My Personal Notes arrow_drop_up