DEV Community

Beatriz Maciel
Beatriz Maciel

Posted on • Edited on

1 1

HackerRank #18 | BigInteger | 🇧🇷

Neste problema simples, é pedido que trabalhemos com números grandes (BigInteger) e que imprimamos a soma de um BI com o outro e a multiplicação dos dois.

Um BigInteger não é um tipo primitivo. Tanto ele quanto um BigNumber ou BigDecimal são classes do pacote java.math e por isso contam com métodos para fazer soma e multiplicação, por exemplo.

O método .add() vem da Interface Set e é utilizado não só como soma, mas também como um tipo de verificação. O método, além de devolver o resultado matemático, também devolve um boolean e não soma valores duplicados ou algum valor null.

Já o método .multiply() vem diretamente do pacote java.math.BigInteger.

=========

Assim, o resultado desse exercício é:

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        BigInteger firstBI = sc.nextBigInteger(); 
        BigInteger secondBI = sc.nextBigInteger();
        sc.close();

        System.out.println(firstBI.add(secondBI));
        System.out.println(firstBI.multiply(secondBI));
    }
}
Enter fullscreen mode Exit fullscreen mode

=========

Referências:

Add : Oracle
Multiply : Oracle

============

Essa publicação faz parte de uma série de exercícios resolvidos em Java no HackerRank. Acesse a série completa:

Top comments (0)

Great read:

Is it Time to go Back to the Monolith?

History repeats itself. Everything old is new again and I’ve been around long enough to see ideas discarded, rediscovered and return triumphantly to overtake the fad. In recent years SQL has made a tremendous comeback from the dead. We love relational databases all over again. I think the Monolith will have its space odyssey moment again. Microservices and serverless are trends pushed by the cloud vendors, designed to sell us more cloud computing resources.

Microservices make very little sense financially for most use cases. Yes, they can ramp down. But when they scale up, they pay the costs in dividends. The increased observability costs alone line the pockets of the “big cloud” vendors.