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

Автор MciprianM, 17 месяцев назад, По-английски
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.
 
 
 
 

 
17 месяцев назад, # | Ответить
  Проголосовать: нравится +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
  •  
    17 месяцев назад, # ^ | Ответить
      Проголосовать: нравится 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.
    •  
      17 месяцев назад, # ^ | Ответить
        Проголосовать: нравится 0 Проголосовать: не нравится

      The complexity is O(n * Log(Q)) ~ O(n ^ 2 * Log(Base))

      there is another method, it is faster, but more complex.


      •  
        17 месяцев назад, # ^ | Ответить
          Проголосовать: нравится 0 Проголосовать: не нравится
        Much More complex? If you are willing please post or send code/explanations(post: here or email:cip.cip3ggg@gmail.com)
  •  
    17 месяцев назад, # ^ | Ответить
      Проголосовать: нравится 0 Проголосовать: не нравится
    I got it now fully. Very nice!