-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLargeFactorial.java
82 lines (68 loc) · 1.97 KB
/
LargeFactorial.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*https://practice.geeksforgeeks.org/problems/factorials-of-large-numbers2508/1*/
/*Prateekshya's Submission*/
class Solution {
static ArrayList<Integer> factorial(int N){
ArrayList<Integer> fact = new ArrayList<Integer>();
//add 1 to the factorial
fact.add(1);
//multiply from 2 to N
for (int i = 2; i <= N; ++i)
multiply(fact,i);
//since the result is stored in reverse order
Collections.reverse(fact);
return fact;
}
static void multiply(ArrayList<Integer> fact, int next)
{
int i = 0;
int totalDigits = fact.size();
int carry = 0;
//for each digit
while (i < totalDigits)
{
//multiply with the number
int product = next * (Integer)fact.get(i);
//add the previous carry
product += carry;
//pick the LSB
int LSB = product%10;
//store the current carry
carry = product/10;
//set the new digit
fact.set(i,LSB);
//increment
++i;
}
//till the carry exists
while (carry > 0)
{
//add the digits one by one
fact.add(carry%10);
carry /= 10;
}
}
}
/*Pratik's Submission*/
class Solution {
static ArrayList<Integer> factorial(int N){
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(1);
int carry = 0;
for(int i=2;i<=N;i++)
{
for(int j=0;j<al.size();j++)
{
int mul = al.get(j)*i+carry;
al.set(j,mul%10);
carry = mul/10;
}
while(carry!=0)
{
al.add(carry%10);
carry/=10;
}
}
Collections.reverse(al);
return a
}
}