-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactorialsOfLargeNumbers.cpp
More file actions
54 lines (48 loc) · 1.39 KB
/
factorialsOfLargeNumbers.cpp
File metadata and controls
54 lines (48 loc) · 1.39 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*Given an integer N, find its factorial.
Example 1:
Input: N = 5
Output: 120
Explanation : 5! = 1*2*3*4*5 = 120
Example 2:
Input: N = 10
Output: 3628800
Explanation :
10! = 1*2*3*4*5*6*7*8*9*10 = 3628800 */
class Solution {
public:
vector<int> factorial(int N)
{
//vector fact will store the digits after each multiplication during factorial
vector<int> fact;
fact.push_back(1);
//multiplying from 2 upto N
for(int i=2;i<=N;i++)
{
multiply(fact,i);
}
//fact will contin digit of finl result in reverse order so we need to reverse it agaian
reverse(fact.begin(),fact.end());
return fact;
}
void multiply(vector<int>& fact,int n)
{
//initially carry will be zero
int carry =0;
for(int i=0;i<fact.size();i++)
{
//number will be multiplied with each digit of current factorial
int num=n*fact[i]+carry;
//last digit of resultant will be stored in vector and
//rest will be passed as carry
fact[i]=num%10;
carry=num/10;
}
//if all digits are multiplied and carry is left
//then add each digit of carry into vector
while(carry)
{
fact.push_back(carry%10);
carry/=10;
}
}
};