Need to design an Algorithm, which should gave $\mathtt{[Output]}$ as per the $\mathtt{[Input]}$ mentioned below.
| $\mathcal{Input}$ | $\mathcal{Output}$ |
|---|---|
| $0$ | $1$ |
| $1$ | $2$ |
| $2$ | $4$ |
| $3$ | $4$ |
| $4$ | $8$ |
| $5$ | $8$ |
| $6$ | $8$ |
| $7$ | $8$ |
| $8$ | $16$ |
| $9$ | $16$ |
| $10$ | $16$ |
| $11$ | $16$ |
| $12$ | $16$ |
| $13$ | $16$ |
| $14$ | $16$ |
| $15$ | $16$ |
Can anyone help me in finding correct formula?
As a try, I prepared this Formula
$$\LARGE\boxed{2^{\lfloor\log_2(x)\rfloor}}$$
where $x$ is the input.
In Code
Math.pow(2, Math.floor(Math.log2(K))
Use this function $$f(0)=1 \quad;\;f(n)= 2^{\lfloor \log_2 n \rfloor+1} \; \text{if} ~ n \ge 1$$