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.
voidextraLongFactorials(intn){vector<int>bigInt;// Extract digits of n to initialise bigInt{stringstreamss;ss<<n;stringnString;ss>>nString;for(charc:nString)bigInt.push_back(c-'0');--n;}for(;n>0;--n){// Calculate the product of each individual digit of bigInt with nvector<int>product;for(unsignedinti=0;i<bigInt.size();++i)product.push_back(bigInt[i]*n);// determine new size of bigInt{size_tprevSize=bigInt.size();stringstreamss;ss<<n;bigInt=vector<int>(prevSize+ss.str().length(),0);}// Add products to bigInt one by oneintsizeDiff=static_cast<int>(bigInt.size()-product.size());intcarryOver=0;for(intj=static_cast<int>(bigInt.size()-1);j>=0;--j){if(j-sizeDiff<0){if(carryOver>0){bigInt[j]=carryOver%10;carryOver/=10;}else{// pop remaining leading 0s, erase() is [first, last)bigInt.erase(bigInt.begin(),bigInt.begin()+j+1);break;}}else{bigInt[j]=(carryOver+product[j-sizeDiff])%10;carryOver=(carryOver+product[j-sizeDiff])/10;}}}for(inti:bigInt)cout<<i;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Extra Long Factorials
You are viewing a single comment's thread. Return to all comments →
My C++ solution: