Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding a subset of numbers that will give you the max sum

How to find a subset of numbers from 2 to 1000 that will give you the max sum under the condition that any two numbers from the subset don't share common prime factors (e.g., 1000 and 500 share the prime factor 2)?

One (maybe easier) variation to the above question: what is the largest number in the subset? We know that 997 is a prime number, and it is easy to rule out 1000 and 998, thus the question becomes whether is 999 in the subset?

like image 484
Daniel Avatar asked Nov 17 '17 10:11

Daniel


People also ask

How do you find the maximum subset?

To count the maximal subset, we use another DP array (called as 'count array') where count[i][j] is maximal of. count[i][j-1]. Here current element is not considered. scount[i- X][j-1] + 1.

How do you find the maximum sum of a subset in an array?

Simple Approach: Run a loop for i from 0 to n – 1, where n is the size of the array. Now, we will run a nested loop for j from i to n – 1 and add the value of the element at index j to a variable currentMax. Lastly, for every subarray, we will check if the currentMax is the maximum sum of all contiguous subarrays.

What is the maximum sum subset?

Maximum subset sum such that no two elements in set have same digit in them. Given an array of N elements. Find the subset of elements which has maximum sum such that no two elements in the subset has common digit present in them. Maximum Sum Subset will be = {45, 223} .

How do you find the maximum and minimum of a subset?

Compute the sum of the maximum element of each subset, and the sum of the minimum element of each subset separately, and then subtract the minimum sum from the maximum to get the answer. The sum of the maximum/ minimum element of each subset can be computed easily by iterating through the elements of each subset.

How to find the maximum sum of integers in an array?

You are given an array of integers A, you need to find the maximum sum that can be obtained by picking some non-empty subset of the array. If there are many such non-empty subsets, choose the one with the maximum number of elements.

How do you find the number of subsets of an array?

SubsetSum (A, n, sum) = 0, if sum > 0 and n == 0 SubsetSum (A, n, sum) = 1, if sum == 0 Here A is array of elements, n is the number of elements of array A and sum is the sum of elements in the subset. Using this dp, you can solve for the number of subsets for the sum.

How do you solve the subset sum problem?

To solve the subset sum problem, use the same DP approach as given in the subset sum problem. To further count the maximal subset, we use another DP array (called as ‘count array’) where count [i] [j] is maximal of


2 Answers

Create a graph with nodes {2, ..., 1000}, and edges when nodes have gcd > 1. Solution to this problem is same as finding maximum-weight independent set problem, which is NP-hard except in some special cases. This graph case doesn't look like a spacial case from list on Wikipedia page or even on this list.

Update

As James suggested it is possible to reduce number of nodes in this graph. Lets say that signature of a number N with prime decomposition p1^k1*...*pn^kn is a tuple (p1, ..., pn).

First reduction is to remove nodes when there is nodes with larger value and same signature. That reduces graph to 607 nodes.

Next reduction is to remove node N with signature (p1, ..., pn) if there are nodes with signatures that is decomposition of (p1, ..., pn) and there sum is >= N. That reduces graph to 277 nodes.

From these nodes 73 are isolated nodes (primes > 500.)

like image 106
Ante Avatar answered Sep 19 '22 13:09

Ante


I don't know the answer to this, it seems non-trivial to me. Here are a few thoughts.

The sum consists of a variable number of summands, say s0 + s1 + ... sk, where si is an integer in the interval [2, 1000]. Now each si has a prime-power factorization si=(p1e1)*(p2e2) ... where ei ≥ 1.

The condition that "that any two numbers from the subset don't share common prime factors" is equivalent to stating that si are pairwise relatively prime, i.e. gcd(si, sj)=1 for i ≠ j. Also equivalently, whenever one summand si contains a prime p that means that no other summand may contain that prime.

So how do you arrange the primes into summands? One simple rule is immediately obvious. All the primes in [500, 1000] can only appear in the sum alone as individual summands. If they are multiplied by anything else, even the smallest prime 2, the product will be too large. So that leaves the task of arranging the smaller primes. And I don't know they best way to do that. For the sake of completeness I'll provide the following short python program that shows one way.

def sieve_prime_set(n):
    # sieve[i] = set(p1, p2, ...pn) for each prime p_i that divides i.

    sieve = [set() for i in range(n + 1)]
    primes = []
    next_prime = 1
    while True:
        # find the next prime
        for i in range(next_prime + 1, len(sieve)):
            if not sieve[i]:
                next_prime = i
                break
        else:
            break

        primes.append(next_prime)
        # sieve out by this prime
        for kp in range(next_prime, n + 1, next_prime):
            sieve[kp].add(next_prime)

    return sieve, primes

def max_sum_strategy1(sieve):
    last = len(sieve) - 1
    summands = [last]
    max_sum = last
    prime_set = sieve[last]
    while last >= 2:
        last -= 1
        if not sieve[last] & prime_set:
            max_sum += last
            prime_set |= sieve[last]
            summands.append(last)

    return max_sum, summands, prime_set

def max_sum_strategy2(primes, n):
    return sum(p ** int(log(n, p)) for p in primes)



if __name__ == '__main__':
    sieve, primes = sieve_prime_set(1000)
    max_sum, _, _ = max_sum_strategy1(sieve)
    print(max_sum)
    print(max_sum_strategy2(primes, 1000))

Output is

84972
81447

showing that "strategy 1" is superior.

Superior, but not necessarily optimal. For example, including 1000 seems good, but it forces us to exclude every other even summand and every summand divisible by 5. If we leave 1000 out but include 998 instead, we get to use another summand that includes 5 in it's prime factorization. But including 998 forces other summands to be excluded. So maximizing the sum is not trivial.

like image 36
President James K. Polk Avatar answered Sep 18 '22 13:09

President James K. Polk