How to calculate the number of coprime subsets of the set 1,2,3,..,n
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Calculating the number of coprime subsets of a set involves a combination of combinatorial counting and an understanding of the properties of numbers. Specifically, a subset of a set is considered coprime if the greatest common divisor (gcd) of all its elements is 1. This article will explain how to calculate the number of coprime subsets for the set .
Steps to Calculate Number of Coprime Subsets
Understanding Coprimality
Two integers are coprime if their gcd is 1. For a subset to be coprime, all pairs of numbers within the subset should be coprime. That is, no subset can have a gcd greater than 1.
Inclusion-Exclusion Principle
To determine the number of coprime subsets, we use the principle of inclusion-exclusion, which allows us to count the number of elements with overlapping properties.
Euler's Totient Function
Euler's Totient Function is crucial for this computation. It provides the number of integers up to that are coprime with . However, to find coprime subsets, one must consider subsets of all sizes, which complicates the direct use of .
Coprime Subset Count Formula
We can derive the number of coprime subsets using a combination of inclusion-exclusion and properties of Euler's Totient Function. Let .
- Initial Subset Count:
The total number of subsets of is (including the empty set). - Exclusion of Non-Coprime Subsets:
A subset is not coprime if all its elements share a common factor greater than 1. By considering all primes , we exclude subsets that are not coprime:
- Inclusion-Exclusion for Non-Coprime Subsets:
Use the inclusion-exclusion principle to properly account for overlaps:
- Valid Coprime Subsets:
Finally, the number of coprime subsets is obtained by subtracting non-coprime subsets derived from the inclusion-exclusion principle from the total subsets .
where ranges over sets of primes dividing some subset of .
Example Calculation
Consider . The set is .
- Total subsets: .
- Identify non-coprime subsets: • subsets divisible by : • subset divisible by :
- Coprime subsets via exclusion: • Exclude subsets: • Coprime subsets:
Hence, there are 11 coprime subsets for the set .
Key Points Summary Table
| Concept | Explanation |
| Coprime | Two integers with gcd of 1 |
| Total subsets | for set |
| Non-coprime subsets | Counted using inclusion-exclusion principle |
| Euler's Totient Function | gives count of coprime numbers up to |
| Final count | Subtract non-coprime from total subsets |
Additional Considerations
• Performance: Computing coprime subsets can get computation-intensive for large . Efficient use of mathematical libraries and optimization techniques like memoization can be beneficial. • Application: This method is relevant for understanding combinatorial structures, coding theory, and network topology where independent interactions are modeled.
By understanding these concepts and using the formula described, you can reliably calculate the number of coprime subsets of any set .
Related reading
- How to calculate the shortest path between two points in a grid
- How to calculate total volume of multiple overlapping cuboids
- How to change edges' weight by designated rule?
- How to check if a box fits into another box any rotations allowed
- How to check if a number 1 is power of 2?
- How to check if a number is a power of 2
- How to check if a number is a power of 2
- How to check if a point is inside an ellipsoid?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.