-
Notifications
You must be signed in to change notification settings - Fork 13
/
SmallFactorials.java
40 lines (33 loc) · 1.04 KB
/
SmallFactorials.java
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
37
38
39
40
/*
* - Problem Link - https://www.hackerearth.com/problem/algorithm/small-factorials/
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
class SmallFactorials {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(System.out);
ArrayList<String> al = new ArrayList<String>(100);
for (int x = 1; x <= 100; x++) {
al.add(calculateFact(x));
}
int tc = Integer.parseInt(reader.readLine());
while (tc-- > 0) {
int num = Integer.parseInt(reader.readLine());
writer.write(al.get(num + 1).toString() + "\n");
}
reader.close();
writer.flush();
writer.close();
}
public static String calculateFact(int sh) {
BigInteger fact = new BigInteger("1");
for (int i = 1; i <= sh; i++)
fact = fact.multiply(new BigInteger(i + ""));
return fact.toString();
}
}