6
I’m solving a story-based solution involving the chessboard. See a piece of history:
"Then Sessa asked for his payment in grain of wheat as follows: One grain of wheat for the first box, two for the second, four for the third, and thus doubling to the sixty-fourth and last tableau box.
What is the total 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 grain, it will exceed the type size int
.
I’ve already tested i
with lower limit and worked. The detail is only in the BigInt
.
How I proceed from now on?
Okay, the two similar answers, but they really helped.
– André Nascimento