Finding all Coprime subset upto a number N

旧巷老猫 提交于 2020-05-08 19:48:25

问题


Suppose I have numbers 1 to N and I want to divide them into subsets based on following criteria:

  1. Each number can be present in only 1 subset.
  2. The elements of the subsets must be mutually coprime.
  3. 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:

  1. Set 1: {all primes up to N}
  2. 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.
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!