I have a large list with numbers n1
in listn1
. I want to add all multiples of each number, if the multiplicand is not in another list listn2
(prime numbers with specific characteristics) and the product is below a maximum max_n
. The multiplicand is in most cases below 10, but can go up to 100,000. My code so far looks like:
s = 0
for n1 in listn1:
s += sum(n1 * i for i in range(1, 1 + max_n // n1) if i not in listn2)
The problem: this approach is sloooow. It takes seconds to calculate listn1
and listn2
, so I know that there are more than a million numbers to add. But I started the summation yesterday evening and it is still running this morning.
Is there a Pythonic way to speed up this summation?