Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Division of a big number of 100 digits stored as string

Tags:

c++

string

bignum

I have a 100 digit number stored as string. I want to divide this number with an integer less than 10. How do I efficiently divide a big integer stored as a string with an integer?

like image 876
Wasim Thabraze Avatar asked Jul 04 '14 18:07

Wasim Thabraze


2 Answers

You can check the big integer library.

You can use this library in a C++ program to do arithmetic on integers of size limited only by your computer's memory. The library provides BigUnsigned and BigInteger classes that represent nonnegative integers and signed integers, respectively. Most of the C++ arithmetic operators are overloaded for these classes, so big-integer calculations are as easy as:

#include "BigIntegerLibrary.hh"

BigInteger a = 65536;
cout << (a * a * a * a * a * a * a * a);

(prints 340282366920938463463374607431768211456)

Also check GMP

like image 66
Rahul Tripathi Avatar answered Oct 18 '22 20:10

Rahul Tripathi


@WasimThabraze - what is your understanding of the longhand division method? Since the divisor is less than 1/2 the size of an integer you can use something like this for each divide:

char array[10] = {9,8,7,6,5,4,3,2,1,0};

void divide(int dvsr)
{
int rem = 0;
int dvnd;
int quot;
int i;
    for(i = 0; i < (sizeof(array)/sizeof(array[0])) ; i++){
        dvnd = (rem * 10) + array[i];
        rem = dvnd % dvsr;
        quot = dvnd / dvsr;
        array[i] = quot;
    }
}

int main(void)
{
    divide(8);
    return (0);
}
like image 31
rcgldr Avatar answered Oct 18 '22 18:10

rcgldr