Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,926 questions

51,859 answers

573 users

How to find all possible combinations of numbers from a list that equal to N in Python

3 Answers

0 votes
def GetCombinationsEqualToN(lst, N, combination = []):
    sm = sum(combination)
     
    if sm == N: 
        print("sum(%s) = %s" % (combination, N))
     
    if sm >= N:
        return
     
    for i in range(len(lst)):
        n = lst[i]
        remaining = lst[i+1:]
        GetCombinationsEqualToN(remaining, N, combination + [n]) 
    
 
lst = [4, 6, 8, 2, 1, 10, 3, 5, 13]
N = 13
 
GetCombinationsEqualToN(lst, N)



'''
run:

sum([4, 6, 2, 1]) = 13
sum([4, 6, 3]) = 13
sum([4, 8, 1]) = 13
sum([4, 1, 3, 5]) = 13
sum([6, 2, 5]) = 13
sum([8, 2, 3]) = 13
sum([8, 5]) = 13
sum([2, 1, 10]) = 13
sum([10, 3]) = 13
sum([13]) = 13

'''

 



answered Oct 14, 2022 by avibootz
edited Oct 15, 2022 by avibootz
0 votes
def GetCombinationsEqualToN(lst, N, combination = []):
    sm = sum(combination)
     
    print("combination:", combination)
     
    if sm == N: 
        print("sum(%s) = %s" % (combination, N))
     
    if sm >= N:
        return
     
    for i in range(len(lst)):
        n = lst[i]
        remaining = lst[i+1:]
        GetCombinationsEqualToN(remaining, N, combination + [n]) 
    
 
lst = [4, 6, 8, 2, 1, 10, 3, 5, 13]
N = 13
 
GetCombinationsEqualToN(lst, N)



'''
run:

combination: []
combination: [4]
combination: [4, 6]
combination: [4, 6, 8]
combination: [4, 6, 2]
combination: [4, 6, 2, 1]
sum([4, 6, 2, 1]) = 13
combination: [4, 6, 2, 10]
combination: [4, 6, 2, 3]
combination: [4, 6, 2, 5]
combination: [4, 6, 2, 13]
combination: [4, 6, 1]
combination: [4, 6, 1, 10]
combination: [4, 6, 1, 3]
combination: [4, 6, 1, 5]
combination: [4, 6, 1, 13]
combination: [4, 6, 10]
combination: [4, 6, 3]
sum([4, 6, 3]) = 13
combination: [4, 6, 5]
combination: [4, 6, 13]
combination: [4, 8]
combination: [4, 8, 2]
combination: [4, 8, 1]
sum([4, 8, 1]) = 13
combination: [4, 8, 10]
combination: [4, 8, 3]
combination: [4, 8, 5]
combination: [4, 8, 13]
combination: [4, 2]
combination: [4, 2, 1]
combination: [4, 2, 1, 10]
combination: [4, 2, 1, 3]
combination: [4, 2, 1, 3, 5]
combination: [4, 2, 1, 3, 13]
combination: [4, 2, 1, 5]
combination: [4, 2, 1, 5, 13]
combination: [4, 2, 1, 13]
combination: [4, 2, 10]
combination: [4, 2, 3]
combination: [4, 2, 3, 5]
combination: [4, 2, 3, 13]
combination: [4, 2, 5]
combination: [4, 2, 5, 13]
combination: [4, 2, 13]
combination: [4, 1]
combination: [4, 1, 10]
combination: [4, 1, 3]
combination: [4, 1, 3, 5]
sum([4, 1, 3, 5]) = 13
combination: [4, 1, 3, 13]
combination: [4, 1, 5]
combination: [4, 1, 5, 13]
combination: [4, 1, 13]
combination: [4, 10]
combination: [4, 3]
combination: [4, 3, 5]
combination: [4, 3, 5, 13]
combination: [4, 3, 13]
combination: [4, 5]
combination: [4, 5, 13]
combination: [4, 13]
combination: [6]
combination: [6, 8]
combination: [6, 2]
combination: [6, 2, 1]
combination: [6, 2, 1, 10]
combination: [6, 2, 1, 3]
combination: [6, 2, 1, 3, 5]
combination: [6, 2, 1, 3, 13]
combination: [6, 2, 1, 5]
combination: [6, 2, 1, 13]
combination: [6, 2, 10]
combination: [6, 2, 3]
combination: [6, 2, 3, 5]
combination: [6, 2, 3, 13]
combination: [6, 2, 5]
sum([6, 2, 5]) = 13
combination: [6, 2, 13]
combination: [6, 1]
combination: [6, 1, 10]
combination: [6, 1, 3]
combination: [6, 1, 3, 5]
combination: [6, 1, 3, 13]
combination: [6, 1, 5]
combination: [6, 1, 5, 13]
combination: [6, 1, 13]
combination: [6, 10]
combination: [6, 3]
combination: [6, 3, 5]
combination: [6, 3, 13]
combination: [6, 5]
combination: [6, 5, 13]
combination: [6, 13]
combination: [8]
combination: [8, 2]
combination: [8, 2, 1]
combination: [8, 2, 1, 10]
combination: [8, 2, 1, 3]
combination: [8, 2, 1, 5]
combination: [8, 2, 1, 13]
combination: [8, 2, 10]
combination: [8, 2, 3]
sum([8, 2, 3]) = 13
combination: [8, 2, 5]
combination: [8, 2, 13]
combination: [8, 1]
combination: [8, 1, 10]
combination: [8, 1, 3]
combination: [8, 1, 3, 5]
combination: [8, 1, 3, 13]
combination: [8, 1, 5]
combination: [8, 1, 13]
combination: [8, 10]
combination: [8, 3]
combination: [8, 3, 5]
combination: [8, 3, 13]
combination: [8, 5]
sum([8, 5]) = 13
combination: [8, 13]
combination: [2]
combination: [2, 1]
combination: [2, 1, 10]
sum([2, 1, 10]) = 13
combination: [2, 1, 3]
combination: [2, 1, 3, 5]
combination: [2, 1, 3, 5, 13]
combination: [2, 1, 3, 13]
combination: [2, 1, 5]
combination: [2, 1, 5, 13]
combination: [2, 1, 13]
combination: [2, 10]
combination: [2, 10, 3]
combination: [2, 10, 5]
combination: [2, 10, 13]
combination: [2, 3]
combination: [2, 3, 5]
combination: [2, 3, 5, 13]
combination: [2, 3, 13]
combination: [2, 5]
combination: [2, 5, 13]
combination: [2, 13]
combination: [1]
combination: [1, 10]
combination: [1, 10, 3]
combination: [1, 10, 5]
combination: [1, 10, 13]
combination: [1, 3]
combination: [1, 3, 5]
combination: [1, 3, 5, 13]
combination: [1, 3, 13]
combination: [1, 5]
combination: [1, 5, 13]
combination: [1, 13]
combination: [10]
combination: [10, 3]
sum([10, 3]) = 13
combination: [10, 5]
combination: [10, 13]
combination: [3]
combination: [3, 5]
combination: [3, 5, 13]
combination: [3, 13]
combination: [5]
combination: [5, 13]
combination: [13]
sum([13]) = 13

'''

 



answered Oct 14, 2022 by avibootz
edited Oct 15, 2022 by avibootz
0 votes
import itertools

lst = [4, 6, 8, 2, 1, 10, 3, 5, 13]
N = 13

result = [combination for i in range(len(lst), 0, -1)
          for combination in itertools.combinations(lst, i)
          if sum(combination) == N]

print(result)




'''
run:

[(4, 6, 2, 1), (4, 1, 3, 5), (4, 6, 3), (4, 8, 1), (6, 2, 5), (8, 2, 3), (2, 1, 10), (8, 5), (10, 3), (13,)]

'''

 



answered Oct 14, 2022 by avibootz
...