I am solving a solution based on the story involving the chessboard. Here's a piece of history:
"Then Sessa asked for her payment in wheat grain as follows: A grain of wheat for the first house on the board, two for the second, four by the third, and thus doubling until the sixty-fourth and last house on the board.
What is the total number of grains?
I started the code like this:
import java.math.BigInteger;
public class Graos {
// Só consegui até este método
public static BigInteger numeroGraos(BigInteger totalGraos) {
//nao sei o que por aqui e retornar.
}
public static void main(String[] args) {
int i=1;
int grao=1;
for (i=1; i<=64; i++) {
totalGraos+=grao;
grao=(grao*2); //o quadro seguinte é duas vezes o anterior (PG).
}
System.out.println ("O total de grãos é " + totalGraos);
}
}
If you use integer for the total grains, it will exceed the size of type int
.
I have tested i
with lower limit and it worked. The detail is only in BigInt
.
How do I proceed from here?