int main(int argc, char* argv[]) { LetsStart (); if (!FoolProof(argc,argv)) return 0; char *fNameA=argv[1]; char *DoIt=argv[2]; char *fNameB=argv[3]; char *fNameOut=argv[4]; char *bin=argv[5]; char *fNameM=argv[6]; long size=0; LongInt a, b; char *ReadFlag=0; char *WriteFlag=0; if(strcmp(bin,"-b")==0) { ReadFlag = "rb"; WriteFlag = "bw"; } else { ReadFlag = "r"; WriteFlag = "w"; } if (!a.Read(ReadFlag, fNameA)) return 0; if (!b.Read(ReadFlag, fNameB)) return 0;; LongInt c; if (DoIt[0]=='-') c=a-b; if (DoIt[0]=='+') c=a+b; if (DoIt[0]=='*') c=a*b; if (DoIt[0]=='/') c=a/b; if (DoIt[0]=='%') c=a%b; if (DoIt[0]=='^') { LongInt mod; if(!mod.Read(ReadFlag, fNameM)) return 0; c=c.Pow(a, b, mod); } //c.Print(); if (!c.WriteFile(WriteFlag, fNameOut)) return 0; return 0; }
void factorial(int n) { LongInt fact = 1; for(int i = 2; i <= n; i++) { fact *= i; } fact.saveTo(OUTPUT); }
my::LongInt my::operator -(my::LongInt const &_a, my::LongInt const &_b) { if (_a.sign == _b.sign){ LongInt result; LongInt a, b; unsigned int digit = Pow(2, CHAR_BIT); unsigned int help; if (_a < _b){ //получаем знак разности и после работаем с числами a и b, причем 0 <= b <= a result.sign = false; if (abs(_a) < abs(_b)){ a = abs(_b); b = abs(_a); } else{ b = abs(_b); a = abs(_a); } } else{ result.sign = true; if (abs(_a) < abs(_b)){ a = abs(_b); b = abs(_a); } else{ b = abs(_b); a = abs(_a); } } result.number = a.number; for(unsigned int i = 0; i < b.number.GetSize(); i++){ if (a.number[i] >= b.number[i]) result.number[i] -= b.number[i]; else{ help = a.number[i] + digit - b.number[i]; result.number[i] = (char) help; int j = i + 1; for (; a.number[j] == 0; j++){ a.number[j] = digit - 1; result.number[j] = digit - 1; } a.number[j]--; result.number[j]--; } } result.DelZero(); return result; } else{ LongInt b(_b); b.sign = !b.sign; return _a + b; } }
my::LongInt my::operator /(my::LongInt const &_a, unsigned char const b) { unsigned int digit = Pow(2, CHAR_BIT); LongInt retVal; unsigned int r = 0; unsigned int help; for (int i = _a.number.GetSize() - 1; i >= 0; i--){ help = _a.number[i] + r * digit; retVal.number[i] = help / b; r = help % b; } retVal.DelZero(); return retVal; }
LongInt LongInt::operator-( const LongInt &rhs ) const { LongInt result; if(negative == rhs.negative) { if ((*this) <= rhs && !negative) { LongInt subtrahend(rhs); subtrahend.convertSign(); subtrahend.negative = 0; result = (*this)+subtrahend; result.negative = 1; } else if ((*this) > rhs && !negative) { LongInt minuend((*this)); minuend.convertSign(); minuend.negative = 0; result = minuend+rhs; result.negative = 1; result.convertSign(); } else if ((*this) <= rhs && negative) { LongInt minuend((*this)); minuend.negative = 0; LongInt subtrahend(rhs); subtrahend.convertSign(); result = minuend+subtrahend; result.negative = 1; } else { LongInt minuend((*this)); minuend.convertSign(); LongInt subtrahend(rhs); subtrahend.negative = 0; result = minuend+subtrahend; result.negative = 1; result.convertSign(); } result.remove0s(); } else { if (negative && !rhs.negative){ LongInt temp(rhs); temp.convertSign(); result = (*this) + (temp); } if (!negative && rhs.negative) { LongInt temp(rhs); temp.convertSign(); result = (*this) +(temp); } } return result; }
// Arithmetic binary operators LongInt LongInt::operator+( const LongInt &rhs ) const { LongInt result; if(negative == rhs.negative) { if (negative) { LongInt left(*this); LongInt right(rhs); left.convertSign(); right.convertSign(); result = left + right; result.convertSign(); return result; } Deque<char> r(rhs.digits); //right side Deque<char> l(digits); //left side int i = 0; int carry = 0; while (i < max(r.size(), l.size())) { int sum; int lval; int rval; (!l.isEmpty()) ? lval = (int)l.removeBack() - 48 : lval = 0; (!r.isEmpty()) ? rval = (int)r.removeBack() - 48 : rval = 0; sum = lval + rval + carry; carry = sum / 10; result.digits.addFront((char)(sum % 10 + 48)); } if (carry) result.digits.addFront('1'); } else { if (rhs.negative) { // this positive rhs negative LongInt temp(rhs); temp.convertSign(); result = operator-(temp); } else { // this negative rhs positive LongInt temp((*this)); temp.convertSign(); result = rhs.operator-(temp); } } return result; }
LongInt LongInt::longMult(LongInt& otherLongInt) { LongInt theAnswer = LongInt(0); if (!this->eqZero() && !otherLongInt.eqZero()) { int thisSize = (int) this->myLongInt.size(); int otherSize = (int) otherLongInt.myLongInt.size(); LongInt multTemp; bool isMultTempValued = false; for (int j = 0; j < otherSize; j++) { // j is the iterator through otherLongInt long long mult2 = otherLongInt.myLongInt[j]; multTemp.setZero_(); for (int k = 0; k <= j-1; k++) { multTemp.myLongInt.push_back(0); } for (int i = 0; i < thisSize; i++) { // i is the iterator through this long long mult1 = this->myLongInt[i]; long long multResult = mult1 * mult2; // There might be overflow from the previous multiplication multResult += multTemp.myLongInt[j+i]; if (!isMultTempValued && (multResult > 0)) isMultTempValued = true; // We check for overflow long exceed = multResult / DIVISOR; if (exceed > 0) multResult %= DIVISOR; // If overflow take remainder multTemp.myLongInt[j+i] = (long) multResult; if ((i != thisSize-1) || (exceed > 0)) multTemp.myLongInt.push_back(exceed); } if (isMultTempValued) multTemp.signType = type_positive; theAnswer = theAnswer + multTemp; } } return theAnswer; }
LongInt LongInt::operator * (const LongInt& obj) const{ //乘法的处理 也要考虑不同的符号 LongInt result; result.n.clear(); result = 0; LongInt a = this->getABS(); LongInt b = obj.getABS(); bool negative = (*this<0 and obj>0) or (*this>0 and obj<0); if (*this==0 or obj==0) return 0; for (int i=0;i<obj.n.size();++i) { LongInt midRes;//临时中间变量 midRes.n.clear(); if (i)//如果i大于0 for (int j=1;j<=i;++j) midRes.n.push_back(0); unsigned long long r = 0;//中间余数 for (int j = 0; ; ++j) { if (r == 0 && j >= n.size() ) break; else{ unsigned long long t = r; if (j < n.size()) t += (long long int)a.n[j] * b.n[i]; midRes.n.push_back(t % _base); r = t / _base; } } result += midRes; } return negative ? result.getOpposite() : result; }
LongInt Algorithm::Strassen (LongInt a, LongInt b) { int n=1; while(n<a.length()) { n<<=1; } n<<=1; QVector< std::complex<double> > fa,fb,res(n); while(!a.isEmpty()) { fa.push_front(a.number.takeFirst()); } while(!b.isEmpty()) { fb.push_front(b.number.takeFirst()); } while(fa.length()<n) { fa.push_back(0); } while(fb.length()<n) { fb.push_back(0); } FFT(fa,0); FFT(fb,0); for(int i=0;i<fa.length();i++) { fa[i]*=fb[i]; } FFT(fa,1); while(!fa.isEmpty()) { a.push_front(fa.takeFirst().real()+0.5); } a.normalization(); return a; }
//二分探商优化 效率比短除法大大提高... LongInt LongInt::operator / (const LongInt &obj) const{ assert(obj!=0 && "devide zero"); bool negative = (*this<0 and obj>0) or (*this>0 and obj<0); LongInt a = this->getABS() , b = obj.getABS(); //两类特殊情况 if(a < b) return 0; if(a == b) return negative ? -1 : 1; LongInt lft = 1, rgt = a; LongInt curSum = lft + a; LongInt middle = curSum.divBy2(); LongInt one = 1; //开始二分 while( not(a >= middle * b and a < (b + middle * b) )){ if(middle * b < a) //探商 lft = middle + one; else rgt = middle; curSum = lft + rgt; middle = curSum.divBy2(); } return negative ? middle.getOpposite() : middle; }
LongInt* Pow(int N) //power { if(N == 1) return this; if(N == 0) { LongInt* result = new LongInt(); result->Clear(); result->intData->head->next->value = 1; return result; } if(N > 1) { LongInt* result; result = this->Multi(this); for(int i = 2; i < N; i++) { result = result->Multi(this); } return result; } }
my::LongInt my::LongInt::sqr() { if (number.GetSize() == 1){ unsigned int help = number[0] * number[0]; LongInt retVal(help); return retVal; } unsigned int n = number.GetSize() / 2; LongInt A(0); LongInt B(0); for (unsigned int i = 0; i < n; i++) A.number[i] = number[i]; A.sign = sign; for (unsigned int i = 0; i < number.GetSize() - n; i++) B.number[i] = number[i + n]; B.sign = sign; LongInt retVal; LongInt help1 = A + B; LongInt help = help1.sqr() - A.sqr() - B.sqr(); help1 = B.sqr(); retVal = A.sqr() + help.NumBaseMultiply(n) + help1.NumBaseMultiply(2 * n); return retVal; }
//各种关系运算符 bool LongInt::operator < (const LongInt &obj) const{ if(n.back()<0 and obj.n.back()>=0) return true; else if(n.back() >=0 and obj.n.back()<0) return false; else if(n.back() <0 and obj.n.back()<0 ) return !(this->getABS() < obj.getABS());//递归 else if(n.back() >= 0 and obj.n.back() >=0){ if(n.size() != obj.n.size()) return n.size() < obj.n.size(); for (int k = n.size() -1 ; k>=0; --k) { if( n[k] != obj.n[k] ) return n[k] < obj.n[k]; } } return false; }
LongInt LongInt::operator - (const LongInt& obj) const{ LongInt result; result.n.clear(); if(*this == obj) result = 0; else{ //两个都是非负数时 if(*this >=0 and obj >= 0){ if(*this > obj){ for (int i = 0, r = 0;; ++i) { if(not r and i>= n.size() and i>= obj.n.size()) break; int t = r; if(i < n.size()) t += n[i]; if(i < obj.n.size()) t -= obj.n[i]; if(t < 0){ t += _base; r = -1; //进位 }else{ r = 0; } result.n.push_back(t); } //除去前位0 while(not result.n.back() and not result.n.empty()) result.n.pop_back(); if(result.n.empty()) result.n.push_back(0); //如果就是0 则补回一个 }else{ result = (obj - *this).getOpposite(); } } else if(*this>=0 and obj<0) result = *this + obj; else if(*this<0 and obj>=0) result = (this->getOpposite() + obj).getOpposite(); else if(*this<0 and obj<0) //递归即可 result = obj.getOpposite() - this->getOpposite(); } return result; }
//各种算法运算符 LongInt LongInt::operator + (const LongInt& obj) const{ LongInt result; result.n.clear(); if(*this >= 0 and obj >= 0){//两个非负整数相加才是真正的相加 否则转移为减法处理 for (int i = 0, r = 0; ; ++i) { if(r==0 and i>=n.size() and i>=obj.n.size()) break; int t = r; if(i < n.size()) t += n[i]; if(i < obj.n.size()) t += obj.n[i]; result.n.push_back(t % _base); r = t / _base; } }else if(*this < 0 and obj < 0) result = (this->getABS() + this->getABS()).getOpposite(); else if(*this < 0 and obj >= 0) result = obj - this->getOpposite(); else if(*this >= 0 and obj < 0) result = *this - obj.getOpposite(); return result; }
LongInt LongInt::operator+(LongInt& int2) { StackAr<int> stack1 = this->intStack; StackAr<int> stack2 = int2.intStack; StackAr<int> tempStack (1000001); LongInt newInt; int curr1; int curr2; int newDigit; int carry = 0; while (!stack1.isEmpty() && !stack2.isEmpty()) { curr1 = stack1.top(); stack1.pop(); curr2 = stack2.top(); stack2.pop(); newDigit = curr1 + curr2 + carry; if (newDigit >= 10) { newDigit -= 10; carry = 1; } else { carry = 0; } tempStack.push(newDigit); } if (!stack1.isEmpty()) { while (!stack1.isEmpty()) { curr1 = stack1.top(); stack1.pop(); newDigit = curr1 + carry; if (newDigit >= 10) { newDigit -= 10; carry = 1; } else { carry = 0; } tempStack.push(newDigit); } } if (!stack2.isEmpty()) { while (!stack2.isEmpty()) { curr2 = stack2.top(); stack2.pop(); newDigit = curr2 + carry; if (newDigit >= 10) { newDigit -= 10; carry = 1; } else { carry = 0; } tempStack.push(newDigit); } } while (!tempStack.isEmpty()) { newDigit = tempStack.top(); tempStack.pop(); newInt.addDigit(newDigit); } return newInt; }
LongInt Algorithm::Toom_Cook(LongInt a, LongInt b) { /*/ a=LongInt("529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450"); b=LongInt("29834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450529834750827340585463450"); qDebug()<<a.length(); LongInt z=Strassen(a,b); /*/ //===T1===========initialization=table=and=stack=== qDebug()<<"Hello Lord of Iteration"; QStack<LongInt> U,V; QStack<LongInt> W; QStack<int> C; int Index,i,j;//for iteration; //=======================Table=========== int k=1; int Q=2,R=1; Table table={4,2}; QVector<Table> qv_table; qv_table<<table<<table; QVector<Table>::iterator iter_table;//=qv_table.begin(); int last_q=4; qDebug()<<qA<<"\"(0)\""<<Q<<R<<table.q<<table.r; while(a.length()>(qv_table.last().q+last_q)) { qDebug()<<"algorithm.cpp:"<<"("+QString::number(k)+")"<<Q<<R<<table.q<<table.r; k++; Q+=R; table.q*=table.r; R++; if(R*R<=Q)//if((R+1)^2>=Q) R++; { table.r*=2; } else R--; last_q=qv_table.last().q; qv_table<<table; } qDebug()<<"algorithm.cpp:"<<"("+QString::number(k)+")"<<Q<<R<<table.q<<table.r; iter_table=qv_table.end(); iter_table--; LongInt numbers[10*iter_table->r+1]; //===================T2============================ C.push(-1); C<<a.number<<-5<<b.number; a.clear(); b.clear(); int forSwitch=1;//iter_table--; //====================Cycle======================== //bool isEnd=0; while(1) { switch(forSwitch) { case 1://T3 { k--; iter_table--; //qDebug()<<"K="<<k; if(!k) { a.clear(); b.clear(); //qDebug()<<"C="<<C; while(!C.isEmpty()) { Q=C.pop(); if(Q>=0) { b.push_front(Q); } else { break; } } //qDebug()<<"C="<<C; while(!C.isEmpty()) { Q=C.pop(); if(Q>=0) { a.push_front(Q); } else { C.push(Q); break; } } //qDebug()<<"algorithm.cpp:"<<a<<"*"<<b<<"="; forSwitch=5; a*=b; //qDebug()<<"algorithm.cpp:"<<a; } else { forSwitch=2; } break; } case 2://T4 T5 { //=====T4========= Index=0; numbers[0].clear(); //qDebug()<<"T4 C"<<C; while(!C.isEmpty()) { Q=C.pop(); if(Q>=0) { if(numbers[Index].length()>=iter_table->q) { //qDebug()<<"algorithm.cpp:("<<Index<<")"<<numbers[Index]; numbers[Index].normalization(); Index++; numbers[Index].clear(); } numbers[Index].push_front(Q); } else { break; } } //qDebug()<<"algorithm.cpp:("<<Index<<")"<<numbers[Index]; R=iter_table->r+Index; //qDebug()<<R; //qDebug()<<Index; for(j=0;j<=R;j++) { a=numbers[Index]; for(i=Index-1;i>=0;i--) { a*=j; a+=numbers[i]; } U<<a; } Index=0; numbers[0].clear(); while(!C.isEmpty()) { Q=C.pop(); if(Q>=0) { if(numbers[Index].length()>=iter_table->q) { //qDebug()<<"algorithm.cpp:("<<Index<<")"<<numbers[Index]; numbers[Index].normalization(); Index++; numbers[Index].clear(); } numbers[Index].push_front(Q); } else { C.push(Q); break; } } //qDebug()<<"algorithm.cpp:("<<Index<<")"<<numbers[Index]; //qDebug()<<Index; for(j=0;j<=R;j++) { a=numbers[Index]; //qDebug()<<"algorithm.cpp:j="<<j; for(i=Index-1;i>=0;i--) { a*=j; a+=numbers[i]; } //qDebug()<<"algorithm.cpp:a="<<a; V<<a; } //qDebug()<<"algorithm.cpp:V="<<V; //=====T5========= if(!U.isEmpty()||!V.isEmpty()) { C<<-2<<V.pop().number<<-5<<U.pop().number; while(!U.isEmpty()||!V.isEmpty()) { C<<-3<<V.pop().number<<-5<<U.pop().number; } } else { //qDebug()<<"Error algorithm.cpp: U or V Empty"; } //qDebug()<<"C="<<C; b.clear(); b.push_front(-4); W<<b; forSwitch=1; break; } case 3://T6 { W<<a; forSwitch=1; //qDebug()<<"algorithm.cpp:a="<<a; break; } case 4://T7 T8 T9 { //qDebug()<<W<<"\n"; //W.clear(); //W<<-4<<10<<304<<1980<<7084<<18526; Index=-1; while (!W.isEmpty()) { //b=W.pop(); Index++; //qDebug()<<"W="<<W; numbers[Index]=W.pop(); //qDebug()<<W.length(); if(numbers[Index]<0) { //qDebug()<<W.length(); Index--; break; } } //qDebug()<<W; //=====T7========= for(j=1;j<=Index;j++) { for(i=0;i<=Index-j;i++) { numbers[i]=(numbers[i]-numbers[i+1])/LongInt(j); //qDebug()<<"numb"<<numbers[i]<<j; } //qDebug()<<numbers[Index-j+1]<<j; } //qDebug()<<numbers[Index-j+1]; /*/ for(i=0;i<=Index;i++) { qDebug()<<"(T7)numbers="<<numbers[i]; } /*/ //=====T8========= //qDebug()<<""; for(j=Index-1;j>=1;j--) { for(i=Index-j;i>=1;i--) { //qDebug()<<j<<i; numbers[i]-=numbers[i-1]*j; } } /*/ for(i=0;i<=Index;i++) { qDebug()<<"(T8)numbers="<<numbers[i]; } /*/ //=====T9========= a=numbers[0]; //qDebug()<<iter_table->q; for(i=1;i<=Index;i++) { a=(a<<iter_table->q)+numbers[i]; //qDebug()<<a<<a.length()<<iter_table->q<<numbers[i]; } //qDebug()<<""; forSwitch=5; break; } case 5://T10 { k++; iter_table++; if(!C.isEmpty()) switch(C.pop()) { case -1: { /*/ qDebug()<<"a= "<<a; qDebug()<<"z= "<<z; if(a==z) { qDebug()<<"a=z"; } /*/ return a; break; } case -2: { W<<a; forSwitch=4; //qDebug()<<"-2"; break; } case -3: { forSwitch=3; //qDebug()<<"-3"; break; } default: { //qDebug()<<"error"; break; } } else { //qDebug()<<"Error algorithm.cpp: C Empty"; } break; } } } }
LongInt* LongInt :: subtract(LongInt *&q) { LongInt *result = new LongInt(); int temp = 0; Node *n1, *n2; n1 = this->l->get_last(); n2 = q->l->get_last(); ListADT *local = new ListADT; ListADT *foreign = new ListADT; if(this->abs(q) == 0) { //foreign has greate absolute value then this so swap them and set the sign local = q->l; foreign = this->l; n1 = local->get_last(); n2 = foreign->get_last(); result->setsign('-'); //cout << "this is less than foreign" << endl; } else { local = this->l; foreign = q->l; n1 = local->get_last(); n2 = foreign->get_last(); } while(!local->is_first(n1) && !foreign->is_first(n2)) { int borrow = 0; int temp = 0; int carry = 0; int top = 0; int bottom = 0; if(!local->is_first(n1)) { n1 = local->next_left(n1); top = local->get_value(n1); } else { top = 0; } if(!foreign->is_first(n2)) { n2 = foreign->next_left(n2); bottom = foreign->get_value(n2); } else { bottom = 0; } temp = (top - bottom - carry); if(temp < 0) { carry = 1; temp += 10000; } else { carry = 0; } result->l->insert_left(temp); } bool complete = false; while(!complete) { int value = local->get_value(n1); if(value == 0) { complete = true; } else { n1 = local->next_left(n1); result->l->insert_left(value); } } return result; }
LongInt LongInt::minusHelper(LongInt& newNumber) { long carryover = 0L; LongInt theAnswer; theAnswer.signType = type_positive; int i; long resultant; if (myLongInt.size() == newNumber.myLongInt.size()){ if (*this > newNumber){ for (i = 0; i < newNumber.myLongInt.size(); i++){ if (myLongInt[i] >= (newNumber.myLongInt[i] + carryover)) { resultant = myLongInt[i] - newNumber.myLongInt[i] - carryover; carryover = 0L; } else { resultant = DIVISOR + myLongInt[i] - newNumber.myLongInt[i] - carryover; carryover = 1L; } theAnswer.myLongInt.push_back(resultant); } } else { for (i = 0; i < newNumber.myLongInt.size(); i++){ if (newNumber.myLongInt[i] >= (myLongInt[i] + carryover)) { resultant = newNumber.myLongInt[i] - myLongInt[i] - carryover; carryover = 0L; } else { resultant = DIVISOR + newNumber.myLongInt[i] - myLongInt[i] - carryover; carryover = 1L; } theAnswer.myLongInt.push_back(resultant); } theAnswer.signType = type_negative; } } else if (myLongInt.size() > newNumber.myLongInt.size()){ for (i = 0; i < newNumber.myLongInt.size(); i++){ if (myLongInt[i] >= (newNumber.myLongInt[i] + carryover)) { resultant = myLongInt[i] - newNumber.myLongInt[i] - carryover; carryover = 0L; } else { resultant = DIVISOR + myLongInt[i] - newNumber.myLongInt[i] - carryover; carryover = 1L; } theAnswer.myLongInt.push_back(resultant); } for (int j = i; j < myLongInt.size(); j++){ if (carryover == 1L && myLongInt[j] == 0L) { resultant = DIVISOR - carryover; carryover = 1L; } else { resultant = myLongInt[j] - carryover; carryover = 0L; } theAnswer.myLongInt.push_back(resultant); } } else { for (i = 0; i < myLongInt.size(); i++){ if (newNumber.myLongInt[i] >= (myLongInt[i] + carryover)) { resultant = newNumber.myLongInt[i] - myLongInt[i] - carryover; carryover = 0L; } else { resultant = DIVISOR + newNumber.myLongInt[i] - myLongInt[i] - carryover; carryover = 1L; } theAnswer.myLongInt.push_back(resultant); } for (int j = i; j < newNumber.myLongInt.size(); j++){ if (carryover == 1L && newNumber.myLongInt[j] == 0L) { resultant = DIVISOR - carryover; carryover = 1L; } else { resultant = newNumber.myLongInt[j] - carryover; carryover = 0L; } theAnswer.myLongInt.push_back(resultant); } theAnswer.signType = type_negative; } // removing zeros bool zero = true; for (int k = (theAnswer.myLongInt.size() - 1); k >= 0 && zero; k--) { if (theAnswer.myLongInt[k] != 0L) { zero = false; } else { theAnswer.myLongInt.erase(theAnswer.myLongInt.end() - 1); } } if (zero) { theAnswer.setZero_(); } return theAnswer; }
int _tmain(int argc, _TCHAR* argv[]) { LongInt* A = new LongInt(); A->SetData("2222"); LongInt* B = new LongInt(); B->SetData("99999999"); LongInt* C = new LongInt(); C->SetData("246813575732"); LongInt* D = new LongInt(); D->SetData("180270361023456789"); cout<<"A = "<<A->GetData()<<endl; cout<<"B = "<<B->GetData()<<endl; cout<<"C = "<<C->GetData()<<endl; cout<<"D = "<<D->GetData()<<endl<<endl; LongInt *E, *F, *G, *H, *I, *J, *K, *L, *M, *N; cout<<"E = A * D = "<<(E = A->Multi(D))->GetData()<<endl; cout<<"F = A^2 = "<<(F = A->Pow(2))->GetData()<<endl; cout<<"G = D^2 = "<<(G = D->Pow(2))->GetData()<<endl; cout<<"H = B * C = "<<(H = B->Multi(C))->GetData()<<endl; cout<<"I = A + D = "<<(I = A->Add(D))->GetData()<<endl; cout<<"J = I^2 = "<<(J = I->Pow(2))->GetData()<<endl; cout<<"K = J - F = "<<(K = J->Minus(F))->GetData()<<endl; cout<<"L = K - G = "<<(L = K->Minus(G))->GetData()<<endl; cout<<"M = L / E = "<<""<<endl; cout<<"N = E^5 = "<<(N = E->Pow(5))->GetData()<<endl; //cout<<"O = H^3 = "<<""<<endl; //cout<<"P = N - O = "<<""<<endl; //cout<<"Q = N^2 = "<<""<<endl; //cout<<"R = O^2 = "<<""<<endl; //cout<<"S = Q - R = "<<""<<endl; //cout<<"T = S / P = "<<""<<endl; //cout<<"U = T - O = "<<""<<endl; //cout<<"V = U - N = "<<""<<endl; //cout<<"W = C^2 = "<<""<<endl; //cout<<"X = B^2 = "<<""<<endl; //cout<<"Y = W - X = "<<""<<endl; //cout<<"Z = C + B = "<<""<<endl; //cout<<"H = Y / Z = "<<""<<endl; //cout<<"F = N^5 = "<<""<<endl; //cout<<"G = E^25 = "<<""<<endl; //cout<<"I = F / N = "<<""<<endl; //cout<<"J = G / I = "<<""<<endl; //cout<<"M = N^10 = "<<""<<endl; //cout<<"P = G^2 = "<<""<<endl; //cout<<"Q = P - M = "<<""<<endl; //cout<<"R = J - M = "<<""<<endl; getchar(); return 0; }
LongInt Algorithm::Karatsuba(LongInt a_1, LongInt b_1) { if(a_1.length()==1 || b_1.length()==1)//&& { return a_1*b_1; } int length; if(a_1.length()!=1 && b_1.length()!=1) { length=(a_1.length() >= b_1.length() ? b_1.length() : a_1.length() )/2; LongInt a_2; a_2.number=a_1.getVector_of_Number(a_1.length()-length); LongInt b_2; b_2.number=b_1.getVector_of_Number(b_1.length()-length); LongInt ab_1,ab_2; ab_1=Karatsuba(a_1,b_1); ab_2=Karatsuba(a_2,b_2); return ( (((Karatsuba(a_1+a_2,b_1+b_2)-ab_1-ab_2)<<length) + ab_2) + (ab_1<<(2*length)) ); } }
int main(int agrc, char **argv) { string a1("2222"); string b1("99999999"); string c1("246813575732"); string d1("180270361023456789"); string ax1("29302390234702973402973420937420973420937420937234872349872934872893472893749287423847"); string az1("-98534342983742987342987339234098230498203894209928374662342342342356723423423"); //INIT THE LONG INTEGERS LongInt *a = new LongInt(); //= new LongInt(); a->initialize(a1); LongInt *b = new LongInt(); b->initialize(b1); LongInt *c = new LongInt(); c->initialize(c1); LongInt *d = new LongInt(); d->initialize(d1); LongInt *ax = new LongInt(); ax->initialize(ax1); LongInt *az = new LongInt(); az->initialize(az1); //PRINT THE LONG INTEGERS cout << "PRINT THE LONG INTEGERS USING PRINT()" << endl; a->print(); b->print(); c->print(); d->print(); ax->print(); az->print(); cout << endl; //PRINT THE RESULTS OF COMPARING THE LONG INTEGERS TO EACH OTHER WITH LESS THAN cout << "COMPARISON OF LONG INTEGERS WITH LESSTHAN()" << endl; //COMPARISON OF A cout << "a less than b: " << a->lessthan(b) << endl; cout << "a less than c: " << a->lessthan(c) << endl; cout << "a less than d: " << a->lessthan(d) << endl; cout << "a less than ax: " << a->lessthan(ax) << endl; cout << "a less than az: " << a->lessthan(az) << endl << endl; //COMPARISON OF B cout << "b less than a: " << b->lessthan(a) << endl; cout << "b less than c: " << b->lessthan(c) << endl; cout << "b less than d: " << b->lessthan(d) << endl; cout << "b less than ax: " << b->lessthan(ax) << endl; cout << "b less than az: " << b->lessthan(az) << endl << endl; //COMPARISON OF C cout << "c less than a: " << c->lessthan(a) << endl; cout << "c less than b: " << c->lessthan(b) << endl; cout << "c less than d: " << c->lessthan(d) << endl; cout << "c less than ax: " << c->lessthan(ax) << endl; cout << "c less than az: " << c->lessthan(az) << endl << endl; //COMPARISON OF D cout << "d less than a: " << d->lessthan(a) << endl; cout << "d less than b: " << d->lessthan(b) << endl; cout << "d less than c: " << d->lessthan(c) << endl; cout << "d less than ax: " << d->lessthan(ax) << endl; cout << "d less than az: " << d->lessthan(az) << endl << endl; //COMPARISON OF AX cout << "ax less than a: " << ax->lessthan(a) << endl; cout << "ax less than b: " << ax->lessthan(b) << endl; cout << "ax less than c: " << ax->lessthan(c) << endl; cout << "ax less than d: " << ax->lessthan(d) << endl; cout << "ax less than az: " << ax->lessthan(az) << endl << endl; //COMPARISON OF AZ cout << "az less than a: " << az->lessthan(a) << endl; cout << "az less than b: " << az->lessthan(b) << endl; cout << "az less than c: " << az->lessthan(c) << endl; cout << "az less than d: " << az->lessthan(d) << endl; cout << "az less than ax: " << az->lessthan(ax) << endl << endl; //PRINT THE RESULTS OF COMPARING THE LONG INTEGERS TO EACH OTHER WITH GREATER THAN //COMPARISON OF A cout << "COMPARISON OF LONG INTEGERS WITH GREATERTHAN()" << endl; cout << "a greater than b: " << a->greaterthan(b) << endl; cout << "a greater than c: " << a->greaterthan(c) << endl; cout << "a greater than d: " << a->greaterthan(d) << endl; cout << "a greater than ax: " << a->greaterthan(ax) << endl; cout << "a greater than az: " << a->greaterthan(az) << endl << endl; //COMPARISON OF B cout << "b greater than a: " << b->greaterthan(a) << endl; cout << "b greater than c: " << b->greaterthan(c) << endl; cout << "b greater than d: " << b->greaterthan(d) << endl; cout << "b greater than ax: " << b->greaterthan(ax) << endl; cout << "b greater than az: " << b->greaterthan(az) << endl << endl; //COMPARISON OF C cout << "c greater than a: " << c->greaterthan(a) << endl; cout << "c greater than b: " << c->greaterthan(b) << endl; cout << "c greater than d: " << c->greaterthan(d) << endl; cout << "c greater than ax: " << c->greaterthan(ax) << endl; cout << "c greater than az: " << c->greaterthan(az) << endl << endl; //COMPARISON OF D cout << "d greater than a: " << d->greaterthan(a) << endl; cout << "d greater than c: " << d->greaterthan(c) << endl; cout << "d greater than b: " << d->greaterthan(b) << endl; cout << "d greater than ax: " << d->greaterthan(ax) << endl; cout << "d greater than az: " << d->greaterthan(az) << endl << endl; //COMPARISON OF AX cout << "ax greater than a: " << ax->greaterthan(a) << endl; cout << "ax greater than c: " << ax->greaterthan(c) << endl; cout << "ax greater than d: " << ax->greaterthan(d) << endl; cout << "ax greater than b: " << ax->greaterthan(b) << endl; cout << "ax greater than az: " << ax->greaterthan(az) << endl << endl; //COMPARISON OF AZ cout << "az greater than a: " << az->greaterthan(a) << endl; cout << "az greater than c: " << az->greaterthan(c) << endl; cout << "az greater than d: " << az->greaterthan(d) << endl; cout << "az greater than b: " << az->greaterthan(b) << endl; cout << "az greater than ax: " << az->greaterthan(ax) << endl << endl; //PRINT THE RESULTS OF COMPARING THE LONG INTEGERS TO EACH OTHER WITH EQUALTO cout << "COMPARISON OF LONG INTEGERS WITH EQUALTO()" << endl; //LONG INTEGER A COMPARED TO THE OTHERS cout << "a equal to a: " << a->equalto(a) << endl; cout << "a equal to b: " << a->equalto(b) << endl; cout << "a equal to c: " << a->equalto(c) << endl; cout << "a equal to d: " << a->equalto(d) << endl; cout << "a equal to ax: " << a->equalto(ax) << endl; cout << "a equal to az: " << a->equalto(az) << endl << endl; //LONG INTEGER B COMPARED TO THE OTHERS cout << "b equal to b: " << b->equalto(b) << endl; cout << "b equal to a: " << b->equalto(a) << endl; cout << "b equal to c: " << b->equalto(c) << endl; cout << "b equal to d: " << b->equalto(d) << endl; cout << "b equal to ax: " << b->equalto(ax) << endl; cout << "b equal to az: " << b->equalto(az) << endl << endl; //LONG INTEGER C COMPARED TO THE OTHERS cout << "c equal to c: " << c->equalto(c) << endl; cout << "c equal to a: " << c->equalto(a) << endl; cout << "c equal to b: " << c->equalto(b) << endl; cout << "c equal to d: " << c->equalto(d) << endl; cout << "c equal to ax: " << c->equalto(ax) << endl; cout << "c equal to az: " << c->equalto(az) << endl << endl; //LONG INTEGER D COMPARED TO THE OTHERS cout << "d equal to d: " << d->equalto(d) << endl; cout << "d equal to a: " << d->equalto(a) << endl; cout << "d equal to b: " << d->equalto(b) << endl; cout << "d equal to c: " << d->equalto(c) << endl; cout << "d equal to ax: " << d->equalto(ax) << endl; cout << "d equal to az: " << d->equalto(az) << endl << endl; //LONG INTEGER AX COMPARED TO THE OTHERS cout << "ax equal to ax: " << ax->equalto(ax) << endl; cout << "ax equal to a: " << ax->equalto(a) << endl; cout << "ax equal to b: " << ax->equalto(b) << endl; cout << "ax equal to c: " << ax->equalto(c) << endl; cout << "ax equal to d: " << ax->equalto(d) << endl; cout << "ax equal to az: " << ax->equalto(az) << endl << endl; //LONG INTEGER AZ COMPARED TO THE OTHERS cout << "az equal to az: " << az->equalto(az) << endl; cout << "az equal to a: " << az->equalto(a) << endl; cout << "az equal to b: " << az->equalto(b) << endl; cout << "az equal to c: " << az->equalto(c) << endl; cout << "az equal to d: " << az->equalto(d) << endl; cout << "az equal to ax: " << az->equalto(ax) << endl << endl; //PRINT THE NUMBER OF DIGITS IN THE LONG INTEGERS cout << "PRINT THE NUMBER OF DIGITS IN THE LONG INTEGERS USING GETDIGITCOUNT()" << endl; cout << "a: " << a->getdigitcount() << endl; cout << "b: " << b->getdigitcount() << endl; cout << "c: " << c->getdigitcount() << endl; cout << "d: " << d->getdigitcount() << endl; cout << "ax: " << ax->getdigitcount() << endl; cout << "az: " << az->getdigitcount() << endl; //PRINT THE OVERFLOW OF B AS AN INTEGER cout << endl << "PRINT THE OVERFLOW OF B AS AN INTEGER USING OVERFLOW()" << endl; int _b = 99999999; int overflow = a->over_flow(_b); cout << overflow << endl; //FREE THE MEMORY HELD THE LONG INTEGERS free(a); free(b); free(c); free(d); free(ax); free(az); return 0; }
LongInt* LongInt :: multiply(LongInt *&foreign) { LongInt *result = new LongInt(); LongInt *temp = new LongInt(); int tmp_result = 0; int carry = 0; int place = 0; int top = 0; int bottom = 0; Node *n1, *n2; n1 = this->l->get_last(); n2 = foreign->l->get_last(); //ListADT *local = new ListADT; //ListADT *foreign = new ListADT; /* if(this->abs(q) == 0) { //foreign has greate absolute value then this so swap them and set the sign local = q->l; foreign = this->l; n1 = local->get_last(); n2 = foreign->get_last(); n2 = foreign->next_left(n2); } else { local = this->l; foreign = q->l; n1 = local->get_last(); n2 = foreign->get_last(); n2 = foreign->next_left(n2); }*/ top = this->l->get_value(n1); bottom = foreign->l->get_value(n2); tmp_result = top * bottom; carry = over_flow(&tmp_result); temp->l->insert_left(tmp_result); while(!this->l->is_first(n1)) { n1 = this->l->next_left(n1); top = this->l->get_value(n1); bottom = foreign->l->get_value(n2); //cout << "top is: " << top << endl; //cout << "bottom is: " << bottom << endl; tmp_result = bottom * top + carry; carry = over_flow(&tmp_result); temp->l->insert_left(tmp_result); } if(carry != 0) temp->l->insert_left(carry); LongInt *empty = new LongInt(); result = temp; while(!foreign->l->is_first(n2)) { n2 = foreign->l->next_left(n2); n1 = this->l->get_last(); bottom = foreign->l->get_value(n2); place = place + 1; temp = empty; while(place != 0) { empty->l->insert_left(0); place = place - 1; } while(!this->l->is_first(n1)) { top = this->l->get_value(n1); tmp_result = bottom * top + carry; carry = over_flow(&tmp_result); temp->l->insert_left(tmp_result); n1 = this->l->next_left(n1); } result = result->add(temp); temp = empty; } if(carry != 0) result->l->insert_left(carry); return result; }