-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtraLongFactorials.java
More file actions
36 lines (29 loc) · 1.01 KB
/
ExtraLongFactorials.java
File metadata and controls
36 lines (29 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* Calculate and print the factorial of a given integer.
For example, if n = 30, we calculate 30 x 29 x ... x 2 x 1 and get 265252859812191058636308480000000. */
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
// Complete the extraLongFactorials function below.
static void extraLongFactorials(int n) {
BigInteger result = BigInteger.valueOf(n);
if (n == 0) {
System.out.println(1);
}
for (int i = n-1; i > 0; --i) {
result = result.multiply(BigInteger.valueOf(i));
}
System.out.println(result);
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int n = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
extraLongFactorials(n);
scanner.close();
}
}