Tricky Sum

Practice this coding question on Coding Gurukul.

Problem statement

An event accountant numbers transactions from 1 through n. Most transaction numbers contribute their own value as a credit, but transactions numbered by powers of two are refunds and contribute the negative of their value. The accountant needs the final balance for several event sizes. Your task Compute the sum of integers from 1 through n, replacing each power-of-two term by its negative value. Important details The number 1 is a power of two because it equals 2 to the power 0, so its contribution is -1. A refunded term is subtracted, not merely omitted. Results may be negative and large positive results require a 64-bit signed integer. Worked example For n = 6, the balance is -1 -2 +3 -4 +5 +6 = 7. For n = 1, it is -1. Extending an event from n = 7 to n = 8 adds -8 rather than +8, because the new transaction number is a power of two.

Input format

Read t, then t integers n.

Output format

Print one balance per line.

Constraints

1 <= t <= 100; 1 <= n <= 1000000000.

Sample input

1
6

Sample output

7

Sign in to run or submit a solution in C++, Java or Python.

Loading Coding Gurukul…