1. 程式人生 > 實用技巧 >BigInteger The smallest value

BigInteger 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.

Do not forget to import all the needed classes.

Just in case: wiki on factorials.

Sample Input 1:

3628799

Sample Output 1:

10

Sample Input 2:

39916800

Sample Output 2:

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

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        String zero = "0";
        String num = scanner.nextLine();
        
        if (num.equals(zero)) {
            System.out.println(0);
        } else {
            BigInteger m = new BigInteger(num);
            BigInteger result = BigInteger.ONE;

            long i = 1;
            while (result.compareTo(m) < 0) {
                i++;
                result = result.multiply(BigInteger.valueOf(i));
            }

            System.out.println(i);
        }
    }
}