top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to multiply two very big numbers without overflow ?

+4 votes
520 views

Say I have given two big numbers i.e. 10^8 and 10^9 what could be the best way to multiply them without facing the overflow issue.

posted Feb 3, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

–1 vote

Multiply two numbers recursively to solve this problem using some mod function.. below is the code segment. We will multiply two numbers eg. calculate half of multiplication and then add it twice and then for calculating halt of multiplication of two num, calculate 1/4 and then add 4 times and so on...

long long  Multiply(long long a,long long b,long long mod)         
{
    long long  x ,y ;
    x=0;
    y= a % mod;
    while (b > 0)
    {
        if (b % 2 == 1)
        {    
            x = (x + y) % mod;
        }
        y = (y * 2) % mod;
        b /= 2;
    }
    return x % mod;
}
answer Feb 6, 2016 by Shivam Kumar Pandey
Similar Questions
+7 votes

Input:
First List: 5->6->3 // represents number 563
Second List: 8->4->2 // represents number 842
Output
Resultant list: 4->7->4->0->4->6 // represents number 474046

0 votes

You are given 2 long integers having n digits each and you are required to multiply them using C.

Assumptions
Numbers are represented in an array of size n .
Calculate the time complexity using traditional divide and conquer

...