Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I find which combinations of a set of numbers will sum to three numbers?

I don’t even know where to even take this – Python? Excel? Just Google it? (FWIW, my numbers are making up an Excel table. I’ve been just trying to select a few and guess it, but no bueno.)

I have these numbers:

826, 583, 212, 976, 385, 198, 662, 133, 212, 583, 211, 519

I want to figure out which sets to pull together to make these numbers:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

1047, 2453, 2000

(both sets add up to 5500).

Any thoughts about how to figure that out?

>Solution :

Try this

from itertools import chain, combinations

def powerset(iterable):
    "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
    s = list(iterable)
    return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))


for p in powerset([826, 583, 212, 976, 385, 198, 662, 133, 212, 583, 211, 519]):
    if sum(p) in [1047, 2453, 2000]:
        print(p)

Output

(385, 662)
(826, 976, 198)
(583, 212, 198, 662, 133, 212)
(212, 198, 662, 133, 212, 583)
(583, 212, 133, 212, 583, 211, 519)
(583, 212, 385, 198, 133, 212, 211, 519)
(212, 385, 198, 133, 212, 583, 211, 519)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading