I would like to generate a sequence in a list, I know how to do this using a for loop, but if I wanted to generate the list such that the previously generated element was included in the next element, how would I do this? I am very unsure
i.e generate the list such that its items were:
where x is just a symbol
[x,(x)*(x+1),(x)*(x+1)*(x+2)]
rather than [x,x+1,x+2]
Any help greatly appreciated!
I like to use a generator for this sort of thing.
def sequence(x, N):
i = 0
result = 1
while i < N:
result *= (x + i)
i += 1
yield result
>>> list(sequence(5, 10))
[5, 30, 210, 1680, 15120, 151200, 1663200, 19958400, 259459200, 3632428800L]
If you have numpy installed, this is faster:
np.multiply.accumulate(np.arange(x, x + N))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With