Header Ad

HackerRank Extra Long Factorials problem solution

In this HackerRank Extra Long Factorials problem, you have Given an integer value Calculate and print the factorial of a given integer.

HackerRank Extra Long Factorials problem solution


Problem solution in Python programming.

def factorial(x):
    if x == 0: return 0
    answer = 1
    while x > 0:
        answer *= x
        x = x - 1
    return answer

if __name__ == '__main__':
    num = int(input())
    print(factorial(num))


Problem solution in Java Programming.

import java.io.*;
import java.util.*;
import java.math.BigInteger;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner in = new Scanner(System.in);
        
        int n = in.nextInt();
        BigInteger x = BigInteger.valueOf(n);
        n = n - 1;
        while(n > 0){
            x = x.multiply(BigInteger.valueOf(n));
            n = n-1;
        }
        
        System.out.println(x.toString());
       
    }
}


Problem solution in C++ programming.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

string multiply(string &num1, string num2) {
    string res;
    int a, b, c, m, n, l, k, sum, carry;
    char d;

    m = num1.size() - 1;
    n = num2.size() - 1;
    carry = 0;
    for (int i = m; i >= 0; i--) {
        for (int j = n; j >= 0; j--) {
            l = res.size() - 1;
            a = num1[i] - '0';
            b = num2[j] - '0';
            k = (m-i) + (n-j);

            if (l >= k) c = res[l-k] - '0';
            else c = 0;

            sum = a * b + c + carry;
            carry = sum / 10;
            d = char(sum % 10 + '0');

            if (l >= k) res[l-k] = d;
            else res.insert(0, &d, 1);

            if (j == 0 && carry) {
                d = char(carry + '0');
                res.insert(0, &d, 1);
                carry = 0;
            }
        }
    }

    return res[0] == '0' ? "0" : res;
}

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int n;
    cin >> n;
    
    string s = "1";
    for (int i = 1; i <= n; ++i) {
        s = multiply(s, to_string(i));
    }
    cout << s << endl;
    return 0;
}


Problem solution in C programming.

#include<stdio.h>
int main()
{
    int t;
    int a[200]; //array will have the capacity to store 200 digits.
    int n,i,j,temp,m,x;
    scanf("%d",&n);
    a[0]=1;  //initializes array with only 1 digit, the digit 1.
    m=1;    // initializes digit counter

    temp = 0; //Initializes carry variable to 0.
    for(i=1;i<=n;i++)
    {
        for(j=0;j<m;j++)
        {
            x = a[j]*i+temp; //x contains the digit by digit product
            a[j]=x%10; //Contains the digit to store in position j
            temp = x/10; //Contains the carry value that will be stored on later indexes
        }
        while(temp>0) //while loop that will store the carry value on array.
        { 
            a[m]=temp%10;
            temp = temp/10;
            m++; // increments digit counter
        }
    }
    for(i=m-1;i>=0;i--) //printing answer
        printf("%d",a[i]);
    return 0;
}


Problem solution in JavaScript programming.

function processData(input) {
    //Enter your code here
    input = parseInt(input);
    
    var product = [1];
    for (var i = 1; i < input + 1; i++) {
        product = bigMultiply(product, i);
        //console.log('product:' + product);
    }
    console.log(product.join(""));
    
    /*
    console.log("big add test");
    var sum = bigAdd([], [1, 4]);
    console.log('sum: %s', sum);
    
    console.log("big mult test");
    var product = bigMultiply([6], 4);
    console.log("product %s", product);
    */
} 

function bigMultiply(numArray, multiplier) {
    var sum = [];
    for (var i = 1; i <= multiplier; i++) {
        sum = bigAdd(sum, numArray);
        //console.log("sum %s, numArray %s, i $s", sum, numArray, i);
    }
    return sum; 
}

function bigAdd(numArray1, numArray2) {
    var longerArray;
    var shorterArray;
    var sum = [];
    
    if (numArray1.length > numArray2.length) {
        longerArray = numArray1;
        shorterArray = numArray2;
    } else {
        longerArray = numArray2;
        shorterArray = numArray1;
    }
  
    // reverse the array orientation so that base^i start from index 0
    longerArray.reverse();
    shorterArray.reverse();
    
    var carry = 0;
    for (var i = 0; i < longerArray.length; i++) {
        var a = shorterArray[i] || 0;
        var b = longerArray[i];
        var c = a + b + carry;
        
        carry = Math.floor(c / 10);
        var digit = c % 10;
        sum.unshift(digit);
    }
    if (carry > 0) {
        sum.unshift(carry);
    }
    
    // reverse the original arrays. Dirty inefficient.
    longerArray.reverse();
    shorterArray.reverse();
   
    return sum;
}

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});


Post a Comment

0 Comments