How to use "BigInteger" type to solve this problem?

6

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?

    
asked by anonymous 18.09.2015 / 15:18

2 answers

5

I do not know if I understood your question correctly,

Here's what I understand: What you want to do:

import java.math.BigInteger;

public class Graos {

     public static BigInteger numeroGraos(int casasDoTabuleiro) {    
         // Inciamos com zero 
         BigInteger numerosDeGrao = BigInteger.valueOf(0L);
         // Inciamos com 1 grão ...
         BigInteger quantidadeDeGrao = BigInteger.valueOf(1L);

         int ponteiro = 0;

         for (ponteiro=1; ponteiro<=casasDoTabuleiro; ponteiro++) {

             // somamos o numeroDeGrao com a quantidadeDeGrao, passando a soma para a variável!
             numerosDeGrao =  numerosDeGrao.add( quantidadeDeGrao  );
             // Multiplicamos a quantidadeDeGrao por 2!
             quantidadeDeGrao =  quantidadeDeGrao.multiply(BigInteger.valueOf(2L) )  ;

         }

         return numerosDeGrao;

    }

    public static void main(String[] args) {


        BigInteger total = numeroGraos(64);
        System.out.println("Total: "+total);


    }

}

Any questions, just comment!

Greetings!

    
18.09.2015 / 15:37
9

That method is not 100% necessary, you can use the BigInteger type instead of the int, the difference is that BigInteger is a class and its objects are immutable, and their methods return a value without changing the object called the method, this makes it a little chatinho to work with, but solves your problem.

import java.math.BigInteger;

public class Graos {
    public static void main(String[] args) {
        BigInteger grao = BigInteger.valueOf(1);
        BigInteger totalGraos = BigInteger.valueOf(0);

        for (int i=1; i<=64; i++) { 
            totalGraos = totalGraos.add(grao);
            grao = grao.multiply(BigInteger.valueOf(2));
        }   
        System.out.println ("O total de grãos é " + totalGraos);    
    }
}

Result:

  

The total grain is 18446744073709551615

    
18.09.2015 / 15:37