-
i know this post is old, but i have the exact same problem that i am working on.. i need to divide 2^512 +1 by another 49 digit number, but so far only got 2 by 2 digits working.. anyone got any ideas?
Code:
// need to add for loops for array a
#include <iostream.h>
int main()
{
const int sizea = 2;
const int sizeb = 2;
int a[sizea] = { 5, 7 };
int b[sizeb] = { 2, 5 };
int counter[sizea] = { 0 };
int remainder = 0;
// for (int i = 0; i < sizea; i+=sizeb)
for (int i = 0; i < sizea; i++)
{
// Do division (if necessary)
int count = 0;
while (a[i] >= b[i])
{
if (a[(i+1)] < b[(i+1)])
{
a[i] = a[i] - 1;
a[(i+1)] = a[(i+1)] + 10;
}
if (a[i]>=b[i])
{
a[i] -= b[i];
a[i + 1] -= b[i + 1];
++count;
}
}
counter[i + 1] = count;
// Handle remainder
if (a[i]==0)
{
remainder += a[i + 1];
a[i] = 0;
}
else
{
a[i + 1] = ( a[i] * 10 ) + a[i + 1];
remainder = a[i + 1];
a[i] = 0;
}
}
// Output
for (int j = 0; j < sizea; ++j)
{
cout << counter[j];
}
cout << " remainder " << remainder << endl;
return 0;
}
-
Next time start your own post, but why don't you check out the algorithms used by gmp for division
http://www.swox.com/gmp/manual/Divis...n%20Algorithms
or even look at the source code that they use
-
thought id carry on the post as it was similar topic....
but anyways....
its part of a uni assignment, where i have to use arrays, so this is what i got so far, if you remove the comment from line 15 and comment out line 16 it works fine for 2 by 2, but i cant get it to work out right for 3 by 2.