You are viewing a single comment's thread. Return to all comments →
string add(string a, string b) { int sza = a.size(), szb = b.size(), carry = 0; //assuming sza >= szb always for (int i = 0; i < sza - szb; i++) b = '0' + b; string ans = b; for (int i = sza - 1; i >= 0; i--) { int k = int(a[i]) + int(b[i]) - 96 + carry; ans[i] = char(k % 10 + 48); if (k / 10) carry = 1; else carry = 0; } if (carry) ans = '1' + ans; return ans; } string multiply(string num, int t) { string ans = ""; if (t == 0) ans = "0"; else if (t == 1) ans = num; else { ans = num; for (int i = 2; i <= t; i++) ans = add(ans, num); } return ans; } void extraLongFactorials(int n) { int k = min(20, n); long long f = 1; for (int i = 2; i <= k; i++) f *= i; string fac = to_string(f); if (n > 20) { for (int i = 21; i <= n; i++) fac = multiply(fac, i); } cout << fac; }
Seems like cookies are disabled on this browser, please enable them to open this website
Extra Long Factorials
You are viewing a single comment's thread. Return to all comments →
C++ Solution