Skip to content

Commit d52680d

Browse files
Long factorial (#1277)
* Added the c++ code * Added the description of the question Commented the description of the question and explained various parts of the code. * added the changes Co-authored-by: Tarun Yadav <[email protected]>
1 parent 2f741ad commit d52680d

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

Array/readme.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ The syntax is:
2222

2323
To create an array, you need to known the length (or size) of the array in advance, and allocate accordingly. Once an array is created, its length is fixed and cannot be changed. At times, it is hard to ascertain the length of an array. Nonetheless, you need to estimate the length and allocate an upper bound. This is probably the major drawback of using an array.
2424

25+
2526
##Questions
2627

2728
* Counting rotations in a sorted array ---> [Python](/Code/Python/Count_of_rotations.py)
29+
* Factorials of big numbers ----> [C++](/Code/C++/long_factorial.cpp)
2830
* Find duplicate in array ----> [C++](/Code/C++/Duplicate_in_array.cpp) | [Java](/Code/Java/dublicate.java)
2931
* Finding All subset ----> [C++](/Code/C++/Finding_all_subset.cpp)
3032
* Insertion_at_start ----> [C++](/Code/C++/insertion_at_start.cpp) | [Java](/Code/Java/insertion_at_start.java) | [Python](/Code/Python/insertion_at_start.py)
@@ -51,4 +53,6 @@ To create an array, you need to known the length (or size) of the array in advan
5153
* Union of two Unsorted Arrays ----> [C++](/Code/C++/Union_of_two_unsorted_array.cpp)
5254
* Unsorted Zig-Zag Array ----> [C++](/Code/C++/unsorted_zig_zag_array.cpp)
5355
* Kth Minimum in array ---->[Python](/Code/Python/Kth_Minimum_in_Array.py)
54-
56+
* Insertion_at_start ----> [C++](/Code/C++/insertion_at_start.cpp) | [Java](/Code/Java/insertion_at_start.java) | [Python](/Code/Python/insertion_at_start.py)
57+
* Kadane's Algorithm ----> [C++](/Code/C++/kadane_algo.cpp)
58+

Code/C++/long_factorial.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*Finding huge factorials such as !100, !1000 is sometimes required but in languages such as C++
2+
the datatypes are not even capable of storing such a big number. We could achieve calculation of
3+
such a number via going back to the basis of how we used to muiltiply two numbers back in school.
4+
We used to muiltiply numbers and would add the summation and get the final result.
5+
This program here uses a vector to store each particular digit in a single index of the vector.
6+
The carry is forwarded as basic maths and in the end the complete result gets stored in the vector*/
7+
8+
#include <vector>
9+
#include <iostream>
10+
11+
using namespace std;
12+
13+
14+
int main() {
15+
int val;
16+
int carry = 0; //Input the number whose factorial is to be calculated
17+
cin >> val;
18+
vector <int> arr(10000, 0); //The size of the vector should be more than the digits present
19+
arr[0] = 1; //in the factorial of the number.
20+
int k = 0;
21+
22+
for(int i = 1; i <= val; i++) {
23+
for(int j = 0;j <= k; j++) {
24+
arr[j] = arr[j] * i + carry;
25+
carry = arr[j] / 10;
26+
arr[j] = arr[j] % 10;
27+
}
28+
while(carry) { //The carries are forwarded in order to get the final result
29+
k++;
30+
arr[k] = carry % 10;
31+
carry /= 10;
32+
}
33+
}
34+
for(int i = k; i >= 0; i--) { //Each digit of the final answer is stored in a single index
35+
cout << arr[i]; //of the vector
36+
}
37+
cout << "\n";
38+
return 0;
39+
}
40+
41+
/*
42+
Test Cases :
43+
44+
1)Input : 10
45+
Output : 3628800
46+
47+
2)Input : 20
48+
Output : 2432902008176640000
49+
50+
3)Input : 30
51+
Output : 265252859812191058636308480000000
52+
53+
Time Complexity: O ( N ) (where N is the number which is the input)
54+
Space Complexity: O ( N ) (where N is the number of digits present in the final answer)
55+
56+
57+
*/

0 commit comments

Comments
 (0)