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

Asked

Viewed 2,368 times

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?

2 answers

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 its methods return a value, without changing the object that called the method, It makes working with him a little boring, but it 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);    
    }
}

Upshot:

The total grain is 18446744073709551615

5


I’m not sure I understand your doubt correctly,

Follow 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!

  • Okay, the two similar answers, but they really helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.