Sort by

recency

|

1290 Discussions

|

  • + 0 comments
    void extraLongFactorials(int n) {
        int factorial[1000]{0}, product{0}, digit{0}, carry{0}, length{1};
        factorial[0]=1;
        for (int i = 2; i <= n; ++i) {
            for (int j = 0; j < length; ++j) {
                product =  i * factorial[j] + carry;
                digit = product % 10;
                factorial[j] = digit;
                carry = product / 10;
            } 
            while (carry) {
                digit = carry % 10;
                factorial[length] = digit;
                ++length;
                carry/=10;
            }
        }
        for (int k = length - 1; k >= 0; --k) {
             std::cout << factorial[k];
        }
    }
    
  • + 0 comments
    from functools import cache
    
    @cache
    def extraLongFactorials(n):
        return n * extraLongFactorials(n-1) if n else 1
    
  • + 0 comments

    Python solution using List

    j=[]
        for i in range(1,(n+1)):
            
            def appendList(val):
                val.append(i)
            appendList(j)
        
        res=math.prod(j)
        print(res)
    
  • + 0 comments

    Simple Java Logic :)

    BigInteger factorial = BigInteger.ONE; for(int i=1;i<=n;i++){ factorial = factorial.multiply(BigInteger.valueOf(i));
    } System.out.println(factorial); }

  • + 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());
    

    }