-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathFactorial_of_very_large_numbers.cpp
70 lines (60 loc) · 1.58 KB
/
Factorial_of_very_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
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
/* C++ program to compute factorial of big numbers
We will we finding factorial of quite big numbers,
which can't be solved with the general approach of recursion */
#include<bits/stdc++.h>
using namespace std;
/* This function calculates product and carry,
`rsize` is size of vector `res`*/
int multiply(int x, vector<int> &res, int rsize){
int carry = 0;
for (int i=0; i<rsize; i++)
{
int product = res[i] * x + carry;
/* Storing the last digit of `prod` in res */
res[i] = product % 10;
carry = product/10;
}
/* Put carry in res and increment the result size */
while (carry)
{
res.push_back(carry%10);
carry = carry/10;
rsize++;
}
return rsize;
}
/* Function finds factorial of large numbers
and prints them */
void largeFactorial(int num)
{
vector<int> res;
res.push_back(1);
int rsize = 1;
/* Applying general factorial formula */
for (int x=2; x<=num; x++)
rsize = multiply(x, res, rsize);
cout << "Factorial of "<< num << " is : ";
for (int i=rsize-1; i>=0; i--)
cout << res[i];
}
/* Driver Function */
int main()
{
int num;
/*Taking input of the number*/
cout << "Enter the number : ";
cin >> num;
largeFactorial(num);
return 0;
}
/*
SAMPLE I/O =>
1.
Enter the number : 51
Factorial of 51 is : 1551118753287382280224243016469303211063259720016986112000000000000
2.
Enter the number : 69
Factorial of 69 is : 171122452428141311372468338881272839092270544893520369393648040923257279754140647424000000000000000
Time complexity : O(n^2)
Space complexity : O(log(n!))
*/