The idea behind the sum of powers of 2 I know that the sum of power of $2$ is $2^{n+1}-1$, and I know the mathematical induction proof. But does anyone know how $2^{n+1}-1$ comes up in the first place.
For example, sum of n numbers is $\frac{n(n+1)}{2}$. The idea is that we replicate the set and put it in a rectangle, hence we can do the trick. What is the logic behind the sum of power of $2$ formula?

There is a combinatorial argument for why
$$
1 + q + q^2 + \dotsb + q^n = \frac{q^{n+1} - 1}{q - 1}
$$
holds for any positive integer $q \neq 1$.
Consider the function $\gamma$ which takes a list
$a = (a_1,a_2,\dotsc,a_{n+1})$ of length $(n+1)$ with components
in $A = \{1,2, \dotsc, q\}$ and "compresses" it by cutting off repeating elements
at the end of the list. For example, if $n = 3$ and $q = 7$,
\begin{align*}
&\gamma(4,1,6,6) = (4, 1, 6),\\
&\gamma(2,2,3,7) = (2,2,3,7),\\
&\gamma(5,5,5,5) = 5,\\
&\gamma(7,4,4,4) = (7,4).
\end{align*}
More precisely, $\gamma$ is defined as
$$
\gamma\colon
\begin{cases}
A^{n+1} \to A \cup B,\\
a \mapsto (a_1,a_2,\dotsc,a_{i(a)})
\end{cases}
$$
where
$$
B = \bigl\{\,
a \in A^k : 2 \leq k \leq n+1 \text{ and } a_{k-1} \neq a_k
\,\bigr\}
$$
and
$$
i(a) =
\begin{cases}
n+1 &\text{if } a_n \neq a_{n+1},\\
\min \{\, j : a_j = a_{j+1} = \dotsb = a_{n+1}\,\} &\text{else.}
\end{cases}
$$
Because $\gamma$ is a bijection, $A^{n+1}$ and $A \cup B$ have the same number of elements. $A^{n+1}$ has $q^{n+1}$ elements and the number of elements in $A \cup B$
equals the number of elements in $A$ plus the number of elements in $B$ (since $A$ and $B$ are disjoint).
The number of elements in $B$ is
$$
q(q-1) + q^2(q-1) + \dotsb + q^n(q-1),
$$
since $q^k(q-1)$ is the number of lists of length $(k+1)$, whose last two elements are distinct. Putting the pieces together, we get
$$
q^{n+1} = q + (q-1)(q + q^2 + \dotsb + q^n),
$$
which is equivalent to the formula in question.
