• + 0 comments

    C++ solution:

    std::string schoolAddition(const std::string& add0, const std::string& add1) {

    std::vector<char> ret_vec;
    int add0_idx = add0.size() - 1;
    int add1_idx = add1.size() - 1;
    int size = max(add0.size(), add1.size());
    int carry = 0;
    while (size > 0) {
    
        int n_0 = add0_idx < 0 ? 0 : add0[add0_idx] - '0';
        int n_1 = add1_idx < 0 ? 0 : add1[add1_idx] - '0';
    
        int ret = n_0 + n_1 + carry;
        carry = ret / 10;
    
        ret_vec.push_back('0' + (ret % 10));
    
        add0_idx--;
        add1_idx--;
        size--;
    }
    
    if (carry > 0) {
        ret_vec.push_back('0' + carry);
    }
    
    std::reverse(ret_vec.begin(), ret_vec.end());
    
    return std::string(ret_vec.data(), ret_vec.size());
    

    }

    std::string schoolMultiplication(std::string num0, std::string num1) { int carry = 0;

    int j_c = 0;
    std::vector<std::string> rets;
    for (int j = num1.size() - 1; j >= 0; j--) {
    
        std::vector<char> tmp_vec;
        int n_1 = num1[j] - '0';
        for (int i = num0.size() - 1; i >= 0; i--) {
            int n_0 = num0[i] - '0';
    
            int ret = n_0 * n_1 + carry;
            int put = ret % 10;
            tmp_vec.push_back('0' + put);
            carry = ret / 10;
        }
    
        if (carry) {
            tmp_vec.push_back('0' + carry);
            carry = 0;
        }
        std::reverse(tmp_vec.begin(), tmp_vec.end());
    
        for (int k = 0; k < j_c; k++) {
            tmp_vec.push_back('0');
        }
    
        j_c++;
        rets.push_back(std::string(tmp_vec.data(), tmp_vec.size()));
    }
    
    std::string final_num = rets[0];
    for (int i = 1; i < rets.size(); i++) {
        final_num = schoolAddition(final_num, rets[i]);
    }
    
    return final_num;
    

    }

    void extraLongFactorials(int n) {

    std::string factorial = std::to_string(n);
    for (int i = n - 1; i > 1; --i) {
        factorial = schoolMultiplication(factorial, std::to_string(i));
    }
    
    printf("%s\n", factorial.c_str());
    

    }