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.
A commented C++ version using the idea of "arithmatic carry" (the same idea this example used) is as following:
voidextraLongFactorials(intn){if(n==1){cout<<1<<endl;return;}vector<int>digits(1,1);for(intcurr(2);curr<=n;++curr){autodigit_count=digits.size();// Every digit times the number.for(inti(0);i<digit_count;++i){digits[i]*=curr;}// From the units to tenths, hundreds...// Perform the carry.for(inti(0);i<digit_count;++i){autocarry=digits[i]/10;digits[i]=digits[i]%10;if(i!=digit_count-1){// Add the carry to the next digit.digits[i+1]+=carry;}else{// New leftmost digit(s).if(carry>0){int&remaining(carry);do{autodigit=remaining%10;remaining/=10;digits.push_back(digit);}while(remaining>0);}}}}// Print out the digits.for(autoit=digits.rbegin();it!=digits.rend();++it){cout<<*it;}}
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 →
A commented C++ version using the idea of "arithmatic carry" (the same idea this example used) is as following: