I'm using Python and
I'm given an array like a = [1, 2, 3, 4]
and I want to find sum of all possible combination multiplications like:
For combinations of 1: 1 + 2 + 3 + 4
For combinations of 2:1*2 + 2*3 + 3*4 + 4*1
.
For combination of 3: 1*2*3 + 1*3*4 + 2*3*4
For combinations of 4: 1*2*3*4
And finally sum of all these sums is my answer. I'm using numpy.prod()
and numpy.sum()
. But it's still too slow. Is there some better algorithm to find the sum quickly?
You can do with
numpy
anditertools
: