Given $K$, find the largest $x$ such that $\sum_{i \leq x} i 2^i \leq K$

61 Views Asked by At

I'm working on a problem that involves the following summation: $$y=\sum_{i=0}^{x}i2^i$$ I need to determine the largest value of $x$ such that $y$ is less than or equal to some integer K. Currently I'm using a lookup table approach which is fine, but I would really like to find and understand a solution that would allow calculation of $x$.

Thank you!

2

There are 2 best solutions below

0
On

Use the idendity

$$\sum_{i=0}^x i2^i=2(2^xx-2^x+1)$$

and calculate the value $x$ with binary search, for example.

0
On

You can compute this sum exactly to get $$y=x2^{x+1}+2$$.