How to calculate 2^n and (2^n)+1 in java?

I'm doing a job where I have to do some calculations with stacks and I need to do tests for powers of two and powers of two plus one, i.e. 1, 2, 3, 5, 8, 9, 16, 17, etc.

The part of the powers of two is done easily, but how do I loop or loop for the powers of two plus one?

Author: LINQ, 2017-04-13

1 answers

Is simple, just do the power calculation and then add 1.

As no more details follows below. The code calculates all the powers of 2 and the powers of 2 added with 1.

Scanner input = new Scanner(System.in);
System.out.print("Digite um número: ");
int numeroEscolhido = input.nextInt(); //número digitado pelo usuário

for(int i = 0; i <= numeroEscolhido; i++){
    int potenciaDe2 = (int)Math.pow(2, i);

    System.out.println(String.format("(2^%s): %s | (2^%s)+1: %s", i, potenciaDe2, i, potenciaDe2 + 1));
}

Entering with the number 5, the output will be:

(2^0): 1 | (2^0)+1: 2  
(2^1): 2 | (2^1)+1: 3  
(2^2): 4 | (2^2)+1: 5  
(2^3): 8 | (2^3)+1: 9  
(2^4): 16 | (2^4)+1: 17  
(2^5): 32 | (2^5)+1: 33

See working on repl.it.

 1
Author: LINQ, 2019-03-12 23:02:16