JetBrains Academy: BigInteger - Kamil-Jankowski/Learning-JAVA GitHub Wiki

JetBrains Academy: BigInteger

An expression with large numbers:

Write a program that reads four large integers (a, b, c, d) and outputs the result of the expression:

(-a) * b + c - d

Input data format: a single line containing four numbers separated by spaces.

Output data format: a single line containing the result.

import java.util.*;
import java.math.BigInteger;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] numbers = scanner.nextLine().split("\\s+");
        
        // -a * b + c - d
        BigInteger aNeg = new BigInteger(numbers[0]).negate();
        BigInteger b = new BigInteger(numbers[1]);
        BigInteger c = new BigInteger(numbers[2]);
        BigInteger d = new BigInteger(numbers[3]);
        BigInteger result = aNeg.multiply(b).add(c).subtract(d);
        
        System.out.println(result);
    }
}

Comparing numbers:

Alice wrote a method for checking if at least two given BigInteger numbers are equal. But she has made several mistakes and the method works incorrectly.

Try to fix it.

import java.math.BigInteger;
import java.util.Scanner;

public class Main {

    /**
     * It returns true if at least two of three given numbers are equal, otherwise - false.
     */
    public static boolean atLeastTwoAreEqual(BigInteger num1, BigInteger num2, BigInteger num3) {
        return num1.equals(num2) || num2.equals(num3) || num1.equals(num3); // fix this line
    }

    /* Do not change code below */
    public static void main(String[] args) {
        final Scanner scanner = new Scanner(System.in);

        final String[] parts = scanner.nextLine().split("\\s+");

        BigInteger num1 = null;
        BigInteger num2 = null;
        BigInteger num3 = null;

        try {
            num1 = new BigInteger(parts[0]);
            num2 = new BigInteger(parts[1]);
            num3 = new BigInteger(parts[2]);
        } catch (Exception e) {
            System.out.println("Can't parse a big integer value");
            e.printStackTrace();
        }

        System.out.println(atLeastTwoAreEqual(num1, num2, num3) ? "YES" : "NO");
    }
}

The smallest value:

A user inputs a long number M. You need to find out what is the smallest long n, so that n! >= M.

Use the BigInteger class to solve the problem. Otherwise, your solution won't pass all the tests.

import java.math.BigInteger;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String m = scanner.nextLine();
        
        BigInteger biggestFactorial = findMaxFactorialLowerThan(new BigInteger(m));
        System.out.println(biggestFactorial);
    }

    private static BigInteger findMaxFactorialLowerThan(BigInteger m) {
        BigInteger factorial = BigInteger.ONE;
        BigInteger n = BigInteger.ZERO;

        while (n.compareTo(m) < 0) {
            n = n.add(BigInteger.ONE);
            factorial = factorial.multiply(n);
            if (factorial.compareTo(m) >= 0) {
                break;
            }
        }
        return n;
    }
}

Double factorial:

Implement a function to compute the double factorial. It is the product of natural numbers of the same parity, not exceeding a given number.

For example: 7!!=7⋅5⋅3⋅1 8!!=8⋅6⋅4⋅2

The function argument can be any non-negative integer.

import java.math.BigInteger;

class DoubleFactorial {
    public static BigInteger calcDoubleFactorial(int input) {
        BigInteger factorial = BigInteger.ONE;
        int n = input;

        while (n > 0) {
            factorial = factorial.multiply(BigInteger.valueOf(n));
            n -= 2;
        }
        return factorial;
    }
}