Created
September 12, 2022 08:12
-
-
Save Nasah-Kuma/fd3f5fbb322de130a2151cba932575d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function add(str1, str2) { | |
let sum = ""; | |
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; | |
let a; | |
let b; | |
let temp; | |
let digitSum; | |
for (let i = 0; i < str1.length; i++) { | |
a = parseInt(str1.charAt(str1.length - 1 - i)); | |
b = parseInt(str2.charAt(str2.length - 1 - i)); | |
b = (b) ? b : 0; | |
temp = (carry + a + b).toString(); | |
digitSum = temp.charAt(temp.length - 1); | |
carry = parseInt(temp.substr(0, temp.length - 1)); | |
carry = (carry) ? carry : 0; | |
sum = (i === str1.length - 1) ? temp + sum : digitSum + sum; | |
} | |
return sum; | |
} | |
function extraLongFactorials(n) { | |
let fact = 1; | |
for (let i = 2; i <= n; i++){ | |
if(Number.isSafeInteger(fact*i)){ | |
fact = fact * i; | |
} | |
else { | |
let factxi = "0"; | |
for(let j = 0; j < i; j++){ | |
factxi = add(factxi,fact.toString()); | |
} | |
fact = factxi; | |
} | |
} | |
return fact; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment