-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathLargeFactorial.java
More file actions
39 lines (37 loc) · 1.08 KB
/
LargeFactorial.java
File metadata and controls
39 lines (37 loc) · 1.08 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
37
38
39
//User function Template for Java
class Solution {
static ArrayList<Integer> factorial(int n)
{
// declare an arrayList
ArrayList<Integer> result = new ArrayList<Integer>();
int size=0,c=0;
// Adding 1 at 0th index
result.add(0,1);
// Updating size
size=1;
// Decalre a variable to traverse numbers from 2 to n
int val=2;
while(val<=n)
{
// Traverse array list from right to left
for(int i=size-1;i>=0;i--)
{
// Update value in arrayList
int temp=result.get(i)*val + c;
// Store the last digit at index and add remaining to carry
result.set(i,temp%10);
// update carry
c=temp/10;
}
// insert carry digit by digit to the begining of the ArrayList
while(c!=0)
{
result.add(0,c%10);
c=c/10;
size++;
}
val++;
}
return result;
}
}