Get all combinations in a range

Hi,
I’m trying to get the following algorithm (done with Python) but in Julia:

from itertools import product
coins = [2, 5, 10, 20, 50, 100]

no_ways = 0
bunch_of_coins = product(*[range(0, 201, i) for i in coins])
for money in bunch_of_coins:
    if sum(money) <= 200:
        no_ways += 1

The closer I get is this one:

using Combinatorics
no_ways = 0
p2 = 1:100
p5 = 1:40
p10 = 1:20
p20 = 1:10
p50 = 1:4
p100 = 1:2

bunch_of_coins = collect(product(p2,p5,p10,p20,p50,p100))
...

But product only gives tuples of 5 elements, and I would need tuples of 100 elements.

Thanks in advance.

This seems to replicate the Python bunch_of_coins:

coins = [2, 5, 10, 20, 50, 100]
bunch_of_coins = Iterators.product([0:i:200 for i in coins]...)

(By the way, the Python version gives tuples of 6 elements, not 100, so I’m not sure if you meant for the Julia version to create tuples of 100 elements.)

1 Like