hi,i have to implement multiplication of two huge integers of 40 or more digits.i've made a function in a class for its implementation but its not working.plz help if u find any logical errors in it.

void HugeInteger::multiplyHugeIntegers(HugeInteger H1,HugeInteger H2)
{
  int carry=0;
  int count=0;          // introduced to place the result in proper place in final answer
  int k;             //index of veryhugeint (the array to store the final result)
  for (int i=max-1;i>=0;i--)
  {
    for (int j=max-1,k=(max+max);j>=0;j--)
    {
      temp[j]=H1.intarray[i]*H2.intarray[j];    //temp[j] and intarray are two int arrays

          temp[j]=temp[j]%10;
          carry=temp[j]/10;
                                                 // veryhugeint is initialized in a constructor in the class
        veryhugeint[k-count-1]+=carry;          //result stored in the final array
      veryhugeint[k-count]+=temp[j];            // result stored in the final array
      count++;
      k--;
    }
  }

.

if you know any other logic or algorithm for its implementation plz reply.thanks

Dani AI

Generated

A few focused notes that will make the posted routine work and be easy to reason about.

First, the root bugs in the shown function: the carry is computed from the value after you already did value % 10 (so carry becomes 0), the placement math with k/count is fragile and off-by-one (highest valid index is 2*max - 1), and existing digits in the result array are not folded into the sum before taking digit/carry (so multi-stage carries never propagate correctly). was right to flag missing declarations/initialization; also and are correct that the polynomial/grade-school view is the right conceptual model and that Karatsuba is an option for very large sizes.

A clear, robust approach (grade-school multiplication) is: allocate result of length 2*max and zero it, iterate both numbers from least-significant digit to most, accumulate product + existing result + carry into a single sum, write sum % 10 to the target cell and set carry = sum / 10, then after the inner loop add any leftover carry into the next cell. A typical implementation looks like:

vector<int> res(2*max, 0);
for (int i = max-1; i >= 0; --i) {
  int carry = 0;
  for (int j = max-1; j >= 0; --j) {
    int sum = H1.intarray[i] * H2.intarray[j] + res[i + j + 1] + carry;
    res[i + j + 1] = sum % 10;
    carry = sum / 10;
  }
  res[i] += carry;
}

Practical tips: size result as exactly 2*max and use index i+j+1 (not 2*max), initialize everything to zero, reset carry each outer iteration, and validate input length (prefer std::string and convert with ch - '0'). For anything larger than a few hundred digits, consider a tested BigInt library or Karatsuba/FFT multiplication for speed — pointed out ready-made classes and mentioned Karatsuba as the next step.

Recommended Answers

All 8 Replies

hi,i have to implement multiplication of two huge integers of 40 or more digits.i've made a function in a class for its implementation but its not working.plz help if u find any logical errors in it.

void HugeInteger::multiplyHugeIntegers(HugeInteger H1,HugeInteger H2)
{
  int carry=0;
  int count=0;          // introduced to place the result in proper place in final answer
  int k;             //index of veryhugeint (the array to store the final result)
  for (int i=max-1;i>=0;i--)
  {
    for (int j=max-1,k=(max+max);j>=0;j--)
    {
      temp[j]=H1.intarray[i]*H2.intarray[j];    //temp[j] and intarray are two int arrays
 
          temp[j]=temp[j]%10;
          carry=temp[j]/10;
                                                 // veryhugeint is initialized in a constructor in the class
        veryhugeint[k-count-1]+=carry;          //result stored in the final array
      veryhugeint[k-count]+=temp[j];            // result stored in the final array
      count++;
      k--;
    }
  }

.

if you know any other logic or algorithm for its implementation plz reply.thanks

Some day I completed a similar problem by converting the huge integer to a polynomial.

eg. 123456

1.10^5 + 2.10^4 + 3.10^3 + 4.10^2 + 5.10^1 + 6.10^6

you can read the huge integer to a string - or array of chars.
then from right to left, convert them as a polynomial item.

etc. it goes on like that.

This was just an idea for you to complete properly.

Some day I completed a similar problem by converting the huge integer to a polynomial.

eg. 123456

1.10^5 + 2.10^4 + 3.10^3 + 4.10^2 + 5.10^1 + 6.10^6

you can read the huge integer to a string - or array of chars.
then from right to left, convert them as a polynomial item.

etc. it goes on like that.

This was just an idea for you to complete properly.

i did not exactly understand what you mean by converting to you mean't converting to one integer and then multiplying it then plz do tell me that can the int data type hold a number as large as 40 or more. or you are referring to some thing else.plz explain.

It might have helped if you had posted the code for the header file...Are max, temp and intarray member variables of the HugeInteger class? Because if they are not, they are not properly defined and initialized in the function...

Member Avatar for Member #46692

It might have helped if you had posted the code for the header file...Are max, temp and intarray member variables of the HugeInteger class? Because if they are not, they are not properly defined and initialized in the function...

Yes, parts of his/her code don't look right.

It might have helped if you had posted the code for the header file...Are max, temp and intarray member variables of the HugeInteger class? Because if they are not, they are not properly defined and initialized in the function...

yes, all temp intarray and veryhugeint are declared in the class. while the max is a global constant variable. i've also made functions of addition, subtraction, and vaious others. all are working fine except multiplication.

const int max=4;
class HugeInteger
{
  private:
    char charray[max];                     //input is in form of string
    int intarray[max];
    int temp[max+10];
    int veryhugeint[max*max];
//-----------------------------------------------------------------------------
  public:
  HugeInteger()
  {                                     //initializing
    for (int i=0;i<max+10;i++)
    {
      temp[i]=0;
    }
    for (i=0;i<max*max;i++)
    {
      veryhugeint[i]=0;
    }
    for (i=0;i<max;i++)
    {
      intarray[i]=0;
    }
  }
//--------------------------------------------------------------------------
    void convert_to_integer()  // the string input converted to intarray  
    {
      for (int i=0;i<max;i++)  
      {
        char ch;
        ch=charray[i];
        switch (ch)
        {
         case '0':intarray[i]=0;break;
         case '1':intarray[i]=1;break;
         case '2':intarray[i]=2;break;
         case '3':intarray[i]=3;break;
         case '4':intarray[i]=4;break;
         case '5':intarray[i]=5;break;
         case '6':intarray[i]=6;break;
         case '7':intarray[i]=7;break;
         case '8':intarray[i]=8;break;
         case '9':intarray[i]=9;break;
        }
      }
    }
//---------------------------------------------------------------------------
    void inputHugeInteger()
    {
      cout << "input huge integer: ";
      cin >> charray;
    }
//---------------------------------------------------------------------------
    void outputHugeInteger()
    {
      cout << "\nthe result is: ";
      for (int i=0;i<max;i++)
      {
        cout << intarray[i];
      }
      cout << endl;
    }
//---------------------------------------------------------------------------
    //prototypes...
    void addHugeIntegers(HugeInteger,HugeInteger);
    void subtractHugeIntegers(HugeInteger,HugeInteger);
    bool isEqualTo(HugeInteger,HugeInteger);
    bool isNotEqualTo(HugeInteger,HugeInteger);
    bool isGreaterThan(HugeInteger,HugeInteger);
    bool isLessThan(HugeInteger,HugeInteger);
    bool isGreaterThanOrEqualTo(HugeInteger,HugeInteger);
    bool isLessThanOrEqualTo(HugeInteger,HugeInteger);
    bool isZero(HugeInteger);
    void multiplyHugeIntegers(HugeInteger H1,HugeInteger H2);
 

}; // end of class

the only header files used are <iostream.h> and <conio.h>.

Yes, parts of his/her code don't look right.

i did'nt understand which part. plz also tell how to correct it...

If using C++, I offer a solution I have already created for using large numbers. Download bigint.h from:

Member Avatar for Member #46692

read the following, take out what you need.

All he seems to be doing, if you ignore the complexity of the karatsuba implementation, is returning the string reversed, putting some zeros on the end, and doing grade school multiplication.

Simple stuff.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.