I'm trying to find the amortized cost per operation in a sequence of n
operations on a data structure in which the ith
operation costs i
if i
is an exact power of 2, and 1 otherwise.
I think I need to find a way to express the sum of the costs up to a number n
, but I'm stuck. I can see that the special, more expensive i
values are occurring father and farther apart:
i = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20...
So, it looks like I have no numbers between the first and second powers of 2, then one number, then 3, then 7. When I looked at this a while, I (correct me if this is off) found that the number of non-powers of 2 between the jth
and kth
powers of 2 is 2^(j-1) - 1
.
But how can I tie this all together into a summation? I can kind of see something over the js
combined with the number of actual powers of 2 themselves, but I'm having trouble uniting it all into a single concept.