We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
But that's half the fun! Here's my JavaScript solution:
functionmain(){varn=parseInt(readLine());letsum=[1]// Length is half max length b/c we're multiplyingconstMAX_LENGTH=Math.floor(String(Number.MAX_SAFE_INTEGER).length/2)-1constMAX_VALUE=Number(Array(MAX_LENGTH+1).join('9'))for(leti=2;i<=n;i++){letcarryover=0for(letj=0;j<sum.length;j++){letnewValue=sum[j]*i+carryoverif(newValue>MAX_VALUE){letnewValueStr=String(newValue)carryover=Number(newValueStr.substr(0,newValueStr.length-MAX_LENGTH))sum[j]=Number(newValueStr.substr(-MAX_LENGTH))}else{carryover=0sum[j]=newValue}}if(carryover!==0)sum.push(carryover)}// Don't left-pad the highest order grouplethighestGroup=sum.pop()// Left pad all other groups with '0'sum=sum.map(v=>Array(MAX_LENGTH+1-String(v).length).join('0')+v)console.log(highestGroup+sum.reverse().join(''))}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Extra Long Factorials
You are viewing a single comment's thread. Return to all comments →
But that's half the fun! Here's my JavaScript solution: