Subset Sum Demo

Are there prime numbers that sum to 42?

Let's say that you want to find out if there is a set a of prime numbers that sum to 42. The subset sum processor can answer this question.

So, the set we're interested in is the prime numbers up to 42. So, we define that set.

prime_numbers = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41]

First, let's fetch all the subset sums of those prime numbers.

import irtools

prime_sums = irtools.complete_subset_sum(prime_numbers)
if(prime_sums is None):
    # Handle Error
    print("error")

Now we have the binary encoding of the prime sums, we can check if 42 is in that binary using the following

result = irtools.subset_sum_result_check(prime_sums, 42)

print("Are there primes that sum to 42? ", result)

Now, if we put that all together:

prime_numbers = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41]

import irtools

prime_sums = irtools.complete_subset_sum(prime_numbers)
if(prime_sums is None):
    # Handle Error
    print("error")

result = irtools.subset_sum_result_check(prime_sums, 42)

print("Are there primes that sum to 42? ", result)

Now, if we run that, it prints:

Are there primes that sum to 42?  True

And that's correct! The subset [37, 3, 2] sums to 42.