3

I was solving a coding problem which boiled down to computing this formula

$A_n = \sum\limits_{i=1}^n 2^i \cdot ((n + 1) - i)$ where $1 < n < 10^9$

Since $n$ is a huge number the linear time solution exceeds the time limit specified.

I am not sure if this can be further simplified into something that doesn't require looping through the entire $n$ intermediate sums and summing them up.

Could you please tell me if this can be further simplified and how.

rtybase
  • 16,907
thebenman
  • 145

1 Answers1

2

I will rely on the shortcuts from this question $$A_n = \sum\limits_{i=1}^n 2^i \cdot \left((n + 1) - i\right)= \sum\limits_{i=1}^n (n + 1)\cdot 2^i - \sum\limits_{i=1}^n i\cdot 2^i=\\ (n+1)\sum\limits_{i=1}^n 2^i -\sum\limits_{i=1}^n i\cdot2^i=\\ (n+1)\left(2^{n+1}-2\right)-\left(n2^{n+2}-(n+1)2^{n+1}+2\right)=\\ 2^{n + 2} - 2n-4$$

rtybase
  • 16,907