Is there a better way to get the value of X after recursively multiplying by list Z?

19 Views Asked by At

If I have a number X and a list of percentages Z, how would I find the value of X after repeatedly multiplying it (and its result) by each percentage value in the list Z?

Naive Way:

X = 100
Z = [0.1, 0.2, 0.3, 0.1]

for z in Z:
    X = X + (X * z)

Is there a better (faster) algorithm to accomplish this?

1

There are 1 best solutions below

0
On BEST ANSWER

I translate your problem as: \begin{align} X_0 &= 100 \\ X_{i+1} &= X_i + X_i Z_i \quad \left( i \in \{0,\dotsc,N-1 \} \right) \end{align}

There is little you can do if the $N$ percentages $Z_i$ have arbitrary values.

$$ X_{i+1} = X_i + X_i Z_i = X_i (1 + Z_i) $$ which leads to $$ X_N = X_0 \prod_{i=0}^{N-1}(1+Z_i) $$