Блог пользователя MciprianM

Автор MciprianM, 14 лет назад, По-английски
Given two large natural numers A and B ( A, B >= 264 ) find their quotient q and remainder r, where A = B * q + r,
0 <=  r < B. How do you solve this problem? Please post comments ( solution, code ). I mention I would be interested in seeing the code for the schoolbook algorithm and in seeing what would be the simplest way to code division without using libraries for Big Integers.
  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

14 лет назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится
int z = 1;
int k = 0;
while (x > y)
{
y = y * 2;
z = z * 2;
k = k + 1;
}

q = 0;
r = 0;
while (k >= 0)
{
if (x >= y)
{
x = x - y;
q = q + z;
}
y = y / 2;
z = z / 2;
k = k - 1;
}

r = x;

q * y + r == x
  • 14 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится
    Hello! Thanks for the algorithm. It seems to me that the final complexity is O(n2) am I right? I have not yet succeeded to understand why it works exactly, but I noticed it works. Tomorrow morning I'll check it out in detail. Someone gave you a minus, maybe they did not understand what you wrote. No worries I gave you a aplus.
  • 14 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится
    I got it now fully. Very nice!