You are viewing a single comment's thread. Return to all comments →
C++ code. I don't think my way to solve this is best but, if anyone need this, you're welcome <3
string operator+(const string& a, const string& b) { int i = a.size() - 1, j = b.size() - 1; string result = ""; int rem = 0; while (i >= 0 && j >= 0) { int x = a[i] - '0' + b[j] - '0' + rem; if (x >= 10) { x -= 10; rem = 1; } else rem = 0; result = to_string(x) + result; i--; j--; } while (i >= 0) { int x = a[i] - '0' + rem; if (x >= 10) { x -= 10; rem = 1; } else rem = 0; result = to_string(x) + result; i--; } while (j >= 0) { int x = b[j] - '0' + rem; if (x >= 10) { x -= 10; rem = 1; } else rem = 0; result = to_string(x) + result; j--; } if (rem == 1) result = to_string(1) + result; return result; } string operator*(const string& a, const string& b) { vector<string> multiple; for (int i = 0; i < a.size(); i++) { int rem = 0; string temp = ""; for (int j = b.size() - 1; j >= 0; j--) { int x = (a[i] - '0') * (b[j] - '0') + rem; if (x >= 10) { rem = x / 10; x %= 10; } else rem = 0; temp = to_string(x) + temp; } if (rem > 0) temp = to_string(rem) + temp; multiple.push_back(temp); } string result; for (int i = 0; i < multiple.size(); i++) { result = result + "0"; result = result + multiple[i]; } return result; } void extraLongFactorials(int n) { string result = "1"; for (int i = 2; i <= n; i++) { string temp = to_string(i); result = result * temp; } std::cout << result << std::endl; }
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++ code. I don't think my way to solve this is best but, if anyone need this, you're welcome <3