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.
I literally wrote an implementation for the BigInteger (just for factorials)
classBigInteger{private:// The number is stored in reverse.std::stringdigits;staticinlineintcharToInteger(constchardigit);staticinlinecharintegerToChar(constintdigit);public:BigInteger();BigInteger(std::stringstringNumber);BigInteger(unsignedlonglongintx);BigInteger(constBigInteger&original);std::stringgetDigits()const;std::stringgetDisplay()const;voidsetDigits(conststd::string&newDigits);intgetDigitAt(intindex)const;intgetNumberOfDigit()const;BigInteger&operator=(constBigInteger&t);BigInteger&operator*=(constBigInteger&operand);};std::stringfactorial(intnumber){BigIntegerresult("1");for(inti=2;i<=number;i++){result*=BigInteger(i);}returnresult.getDisplay();}inlineintBigInteger::charToInteger(constchardigit){assert(digit>='0'&&digit<='9');return(int)(digit-'0');}inlinecharBigInteger::integerToChar(constintdigit){assert(digit>=0&&digit<=9);return(char)('0'+digit);}BigInteger::BigInteger(){this->digits="0";}BigInteger::BigInteger(std::stringstringNumber){std::reverse(stringNumber.begin(),stringNumber.end());this->digits=stringNumber;}BigInteger::BigInteger(unsignedlonglongx){std::stringdigitString=std::to_string(x);std::reverse(digitString.begin(),digitString.end());this->digits=digitString;}BigInteger::BigInteger(constBigInteger&original){this->digits=original.getDigits();}std::stringBigInteger::getDigits()const{returnthis->digits;}std::stringBigInteger::getDisplay()const{std::stringdisplayDigits=this->getDigits();std::reverse(displayDigits.begin(),displayDigits.end());returndisplayDigits;}voidBigInteger::setDigits(conststd::string&newDigits){this->digits=newDigits;}intBigInteger::getDigitAt(intindex)const{assert(index>=0&&index<this->digits.length());returncharToInteger(this->digits.at(index));}intBigInteger::getNumberOfDigit()const{returnthis->digits.length();}BigInteger&BigInteger::operator=(constBigInteger&t){if(this!=&t){this->digits=t.getDigits();}return*this;}BigInteger&BigInteger::operator*=(constBigInteger&operand){intleftLength=this->getNumberOfDigit();intrightLength=operand.getNumberOfDigit();intfinalLength=leftLength+rightLength;std::vector<int>accumulator(finalLength);for(inti=0;i<leftLength;i++){for(intj=0;j<rightLength;j++){accumulator[i+j]+=this->getDigitAt(i)*operand.getDigitAt(j);}}std::stringresultDigits;intcarry=0;for(intresult:accumulator){intcurrentResult=result+carry;resultDigits.push_back(integerToChar(currentResult%10));carry=currentResult/10;}while(carry!=0){resultDigits.push_back(carry%10);carry/=10;}while(resultDigits.back()=='0'){resultDigits.pop_back();}this->setDigits(resultDigits);return*this;}voidextraLongFactorials(intn){BigIntegerresult("1");for(inti=2;i<=n;i++){result*=BigInteger(i);}cout<<result.getDisplay()<<'\n';}
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 →
I literally wrote an implementation for the BigInteger (just for factorials)