Given an integer ‘N’, you are supposed to return the factorial of the given integer in the form of a string.
Input format :
The first line contains a single integer ‘T’ denoting the number of test cases.
Each test case contains a single line with a single integer ‘N’ denoting the given number.
Output format :
For each test case, print a string denoting the factorial of the given number in a separate line.
Note :
You do not need to print anything; it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 50
1 <= N <= 500
Time Limit: 1 sec.
2
3
4
6
24
In the first test case, the factorial of 3 is 1x2x3 = 6.
In the second test case, the factorial of 4 is 1x2x3x4 = 24.
2
5
1
120
1
Think about a data structure to store the factorials for large integers.
Since the factorials of large integers exceed 10^18 they cannot be stored in any variable of primitive data type, hence the idea is to use an array to store the factorial of the numbers.
Steps are as follows:
O(N*M), where ‘N’ is the given integer and ‘M’ is the size of the result array.
Since we are going through ‘N’ iterations and in the helper function it takes ‘M’ iterations so the overall time complexity will be O(N*M).
O(M), where ‘M’ is the size of the result array.
To store the result ‘M’ space is required, so the overall space complexity will be O(M).