-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtra_Long_Factorials.cpp
52 lines (45 loc) · 1.14 KB
/
Extra_Long_Factorials.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
/**
* Title : Extra_Long_Factorials.cpp
* Author : Tridib Samanta
* Created : 24-03-2020
* Link : https://www.hackerrank.com/challenges/extra-long-factorials/problem
**/
#include <bits/stdc++.h>
using namespace std;
void extraLongFactorials(int n) {
vector<int> result;
result.push_back(1);
for (int i = 2; i <= n; i++)
{
for (auto &digit : result)
digit = digit * i;
int carry = 0;
for (auto &digit : result)
{
digit += carry;
if (digit > 9)
{
int new_digit = digit % 10;
carry = digit / 10;
digit = new_digit;
}
else carry = 0;
}
while (carry)
{
result.push_back(carry % 10);
carry /= 10;
}
}
for (vector<int>::reverse_iterator rit = result.rbegin(); rit != result.rend(); rit++)
cout << *rit;
cout << endl;
}
int main()
{
int n;
cin >> n;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
extraLongFactorials(n);
return 0;
}