5 Best Ways to Program to Find Number of Combinations of Coins to Reach Target in Python

Rate this post

πŸ’‘ Problem Formulation: This article provides a comprehensive guide on how to compute the number of ways to make a certain amount of money with coins of fixed denominations in Python. For example, given the coins [1, 2, 5] and the target amount 5, the program should output 4, as there are four combinations to make 5: [5], [2, 2, 1], [2, 1, 1, 1], and [1, 1, 1, 1, 1].

Method 1: Recursive Approach

The Recursive Approach is a fundamental method for finding the number of combinations to reach a target amount. The function explores all possible combinations by successively considering each coin denomination and reducing the problem into a smaller subproblem, which combines the results at the end.

Here’s an example:

def coin_combinations(coins, target):
    if target == 0:
        return 1
    if target = 1:
        return 0
    return coin_combinations(coins[:-1], target) + coin_combinations(coins, target - coins[-1])

print(coin_combinations([1, 2, 5], 5))

Output: 4

The code defines the coin_combinations() function, which recursively computes the number of ways to make the target amount using the available coins. The function uses the inclusion and exclusion of the last coin in the list to calculate all possible combinations. The base cases consider if the target is met, overstepped, or if there are no more coins to use.

Method 2: Dynamic Programming – Memoization

Dynamic Programming via Memoization optimizes the recursive approach by storing intermediate results, preventing the re-calculation of the same subproblems. This method offers significant performance gains on large inputs by utilizing a cache.

Here’s an example:

def coin_combinations_memo(coins, target, cache=None):
    if cache is None:
        cache = {}
    if target in cache:
        return cache[target]
    if target == 0:
        return 1
    if target = 1:
        return 0
    cache[target] = coin_combinations_memo(coins[:-1], target, cache) + coin_combinations_memo(coins, target - coins[-1], cache)
    return cache[target]

print(coin_combinations_memo([1, 2, 5], 5))

Output: 4

The coin_combinations_memo() function is an enhanced variation of the recursive method which leverages a cache to store the results of subproblems. By checking the cache before performing recursive calls, it avoids redundant computations, efficiently scaling to more significant inputs and more complex denomination arrays.

Method 3: Dynamic Programming – Tabulation

Dynamic Programming via Tabulation builds upon the idea of Memoization but uses iterative bottom-up logic, filling up a table based on the previously-computed values. This method is non-recursive and typically achieves better space complexity.

Here’s an example:

def coin_combinations_tab(coins, target):
    table = [0] * (target + 1)
    table[0] = 1
    for coin in coins:
        for x in range(coin, target + 1):
            table[x] += table[x - coin]
    return table[target]

print(coin_combinations_tab([1, 2, 5], 5))

Output: 4

This code snippet creates a list table with target + 1 zeros, with the first element as 1, signifying the base case where the target amount is 0. Then, it iterates over the coins and target values, summing up ways to make each value up to the target. This method efficiently computes the answer with only one pass through the data.

Method 4: Using a Python Library – itertools

Python’s itertools library can be used to generate combinations of coin denominations. Although not the most efficient method for large targets due to its combinatorial nature, it can be a quick solution for small sets of coins and targets.

Here’s an example:

import itertools

def coin_combinations_itertools(coins, target):
    ways = 0
    for i in range(1, target+1):
        for combination in itertools.combinations_with_replacement(coins, i):
            if sum(combination) == target:
                ways += 1
    return ways

print(coin_combinations_itertools([1, 2, 5], 5))

Output: 4

The coin_combinations_itertools() function uses itertools.combinations_with_replacement() to iterate through all possible combinations of coin denominations, incrementing a counter every time a combination sums up to the target. This method provides a straightforward but potentially slow approach suitable for specific use cases.

Bonus One-Liner Method 5: List Comprehension with itertools

For the enthusiasts of concise code, Python list comprehensions combined with itertools can solve the problem in a one-liner. It’s elegant but may suffer from performance issues for the same reasons as Method 4.

Here’s an example:

from itertools import combinations_with_replacement

print(len([c for i in range(1, 6) for c in combinations_with_replacement([1, 2, 5], i) if sum(c) == 5]))

Output: 4

This one-liner uses a nested list comprehension to iterate over the range of coin numbers, create combinations, filter them based on their sum, and count the total number. Though compact, readability and efficiency considerations should be taken into account when using this approach.


  • Method 1: Recursive Approach. Intuitive; works well with small input sizes. Inefficient for larger targets due to redundant calculations.
  • Method 2: Dynamic Programming – Memoization. Optimizes recursive calls by caching. More scalable for bigger amounts but still uses recursion, which can be memory-intensive.
  • Method 3: Dynamic Programming – Tabulation. Iterative; optimal in space and time complexity. Non-recursive and generally the most recommended approach for large inputs.
  • Method 4: Using Python Library – itertools. Straightforward to implement. Not suitable for large targets due to combinatorial explosion.
  • Method 5: List Comprehension with itertools. Elegant one-liner; best for small-scale problems or quick demonstrations. Can be inefficient and challenging to understand for complex problems.