Figure 2: Pseudocode to divide two BigNums


div(BigNum a, b, q, remainder){
  int position =q.exp=a.exp-b.exp;
  int qGuess //will hold a single digit of q

  int m = b.exp;
  x = a.exp - m;
  while(a>b && x>=0){
     //let atemp be the m leading digits of a
     atemp = a[a.exp..x]

     //make a guess at a digit of q
     qGuess = guess(aTemp, b)

     aTemp = aTemp - qGuess*b;
     while(aTemp is negative){  
     //we've subtracted too much;
     //fix qGuess and adjust atemp
       qGuess--;
       aTemp += b;
     }

     //we've found a digit of q
     q[position--] = qGuess;
  
     //replace leading m digits of a
     //with atemp and note a.exp
     //may be 0 and will need to be adjusted
     a[a.exp..a.exp-m] = aTemp
     while(!a.n[a.exp])a.exp--;
     x--;

     //loop until all digits of q are found
  }
  remainder = a
}