How to solve the below scenario?

53 Views Asked by At

How can extract the individual no from its total sum for below case ?

$$ 2^1+2^3+2^4+2^8+2^{13}+2^{17}=139546 $$

in the above expression i have only value 139546 but no knowledge about no of number in left hand side in this case how can extract the individual numbers like $$ 2^1, 2^3, 2^4, ^8, 2^{13}, 2^{17}$$ from total sum 139546.

Thanks,

1

There are 1 best solutions below

0
On BEST ANSWER

Take a look at this. Specifically here is an algorithm for finding it:

if (n == 0) then
    return (0)
end
k = floor(log_2(n))
a_k = 1
n = n - 2^k
k = k - 1
while n > 0
    if (n >= 2^k) then
        n = n - 2^k
        a_k = 1
    else
        a_k = 0
    end
    k = k - 1
end
return a = (a_0, ..., a_k)

Then $$n = \sum_{i=0}^k a_i 2^i$$ so your numbers are precisely the $2^i$ for wich $a_i = 1$.