[알고리즘] n! 합구하기 - Extra Long Factorials

매번 개발이 새롭다·2022년 10월 16일
0

문제

한글

n! = n x (n – 1) x (n – 2) x . . . x 3 x 2 x 1의 값을 구해서 출력해라

원본

The factorial of the integer n, written n!, is defined as:

n! = n x (n – 1) x (n – 2) x . . . x 3 x 2 x 1

Calculate and print the factorial of a given integer.

For example, if n = 30, we calculate 30 x 29 x 28 x . . . x 2 x 1 and get 26525285981219105636308480000000.

Function Description

Complete the extraLongFactorials function in the editor below. It should print the result and return.

extraLongFactorials has the following parameter(s):

n: an integer
Note: Factorials of n > 20 can’t be stored even in a 64 – bit long long variable. Big integers must be used for such calculations. Languages like Java, Python, Ruby etc. can handle big integers, but we need to write additional code in C/C++ to handle huge values.

We recommend solving this challenge using BigIntegers.

Input Format

Input consists of a single integer n

Constraints

1 <= n <= 100

Output Format

Print the factorial of n.

Sample Input

25

Sample Output

15511210043330985984000000

Explanation

25! = 25 x 24 x 23 x . . . x 3 x 2 x 1

솔루션

  • 일단 문제가 너무 쉽기 때문에 코드만
import java.io.*;
import java.math.BigInteger;

class Result {

    public static void extraLongFactorials(int n) {
        BigInteger sum = BigInteger.valueOf(1);
        for( int i = 1 ; i <= n ; i++ ) {
            sum = sum.multiply(BigInteger.valueOf(i));
        }
        System.out.println(sum.toString());
    }

    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

        int n = Integer.parseInt(bufferedReader.readLine().trim());

        Result.extraLongFactorials(n);

        bufferedReader.close();
    }
}
profile
기억력이 좋지 않은 개발자, 직장인 그리고 꿈이 있다.

0개의 댓글