问题
Suppose I have numbers 1 to N and I want to divide them into subsets based on following criteria:
- Each number can be present in only 1 subset.
- The elements of the subsets must be mutually coprime.
- Minimizing the total number of subsets.
My approach to it is by finding all primes up to N using Sieve of Eratosthenes and then dividing them accordingly in subsets. For example for N=5, I can have two subsets at minimum {1,2,3,5} and {4}. But I am unsure how to distribute the elements in subsets so that each subset has mutually coprime elements. Here's my approach stepwise:
- Set 1: {all primes up to N}
- Set 2: {2k,3k,5k...pk} where p is a prime and pk < N. For different values of k, we can form different sets till 2k< N.
- Rest of the elements
The problem is how to get elements in step 3 to be mutually coprime in the subset Can someone suggest a better approach on how to implement it and flaws in my logic?
回答1:
It seems like it's a trick question. No two even numbers can be in the same subset, so the minimal number of subsets is floor(n/2)
If n is even, you can easily achieve the bound with subsets {2i+1, 2i+2}. For n odd, you do the same, but put {n-2, n-1, n} in the last subset. Note that adjacent numbers are always coprime, and n,n-2 are coprime when n is odd.
来源:https://stackoverflow.com/questions/61024026/finding-all-coprime-subset-upto-a-number-n