-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfactorial_of_large_numbers.cpp
31 lines (29 loc) · 1.01 KB
/
factorial_of_large_numbers.cpp
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
// problem link: https://practice.geeksforgeeks.org/problems/factorials-of-large-numbers2508/1#
// since large numbers' factorial cannot be stored in any data type and there will be an overflow rather
// so we store every element of factorial as a single element of array and later print it
vector<int> factorial(int n){
// the solution is basically based on the multiplication which we used to do primary school
vector<int> vec;
vec.push_back(1);
for(int i=n; i>1; i--)
{
int ans=1;
int carry=0;
int len=vec.size(),temp;
for(int j=0; j<len; j++)
{
ans=i*vec[j];
ans=ans+carry;
vec[j]=ans%10;
carry=ans/10;
}
while(carry != 0)
{
temp=carry%10;
vec.push_back(temp);
carry=carry/10;
}
}
reverse(vec.begin(),vec.end());
return vec;
}