Skip to content

Instantly share code, notes, and snippets.

@SackeyDavid
Created September 29, 2021 22:14
Show Gist options
  • Save SackeyDavid/6bb8d70bedc55577d4576fff32236854 to your computer and use it in GitHub Desktop.
Save SackeyDavid/6bb8d70bedc55577d4576fff32236854 to your computer and use it in GitHub Desktop.
Turing Coding Challenge Extra Long Factorial
function extraLongFactorials(n) {
// check constraints
if(!(n >= 1 && n <= 100 )) return 0;
let fact = 1;
for (let i = 2; i <= n; i++){
if(Number.isSafeInteger(fact*i)){
fact = fact * i;
}
else {
//fact = fact + fact + .. i times
let factxi = "0"; // this is (fact * i) for us.
for(let j = 0; j < i; j++){
factxi = add(factxi,fact.toString());
}
fact = factxi; // update value of fact before continuing the loop.
}
}
console.log(fact);
function add(str1, str2) {
let sum = ""; // our result will be stored in a string.
// we'll need these in the program many times.
let str1Length = str1.length;
let str2Length = str2.length;
// if s2 is longer than s1, swap them.
if(str2Length > str1Length ){
let temp = str2;
str2 = str1;
str1 = temp;
}
let carry = 0; // number that is carried to next decimal place, initially zero.
let a;
let b;
let temp;
let digitSum;
for (let i = 0; i < str1.length; i++) {
a = parseInt(str1.charAt(str1.length - 1 - i)); // get ith digit of str1 from right, we store it in a
b = parseInt(str2.charAt(str2.length - 1 - i)); // get ith digit of str2 from right, we store it in b
b = (b) ? b : 0; // make sure b is a number, (this is useful in case, str2 is shorter than str1
temp = (carry + a + b).toString(); // add a and b along with carry, store it in a temp string.
digitSum = temp.charAt(temp.length - 1); //
carry = parseInt(temp.substr(0, temp.length - 1)); // split the string into carry and digitSum ( least significant digit of abSum.
carry = (carry) ? carry : 0; // if carry is not number, make it zero.
sum = (i === str1.length - 1) ? temp + sum : digitSum + sum; // append digitSum to 'sum'. If we reach leftmost digit, append abSum which includes carry too.
}
return sum; // return sum
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment