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.
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 consists of a single integer n
1 <= n <= 100
Print the factorial of n.
25
15511210043330985984000000
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();
}
}