Advertisement
maxim_shlyahtin

DIV

Nov 7th, 2023 (edited)
494
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. #include "DIV_NN_Dk.h"
  2. #include "../COM_NN_D/COM_NN_D.h"
  3. #include "../MUL_ND_N/MUL_ND_N.h"
  4. #include "../MUL_Nk_N/MUL_Nk_N.h"
  5.  
  6. Natural* DIV_NN_Dk::get(Natural* firstOperand, Natural* secondOperand) const{
  7.     size_t firstOperandLen = firstOperand->len();
  8.     size_t secondOperandLen = secondOperand->len();
  9.     Natural firstDigit("1");
  10.     size_t k;
  11.     if(firstOperandLen < secondOperandLen){
  12.         return new Natural("0"); // the second operand is supposed to be smaller than the first
  13.     } else if(COM_NN_D().get(firstOperand, secondOperand)->get(0) == 0) {
  14.         return new Natural("1"); // the operands are equal
  15.     } else {
  16.         //add check first digit(check how std::copy works)
  17.         bool flag = false;
  18.         for(k = firstOperandLen - 1; k >= 0; --k){// k ?= firstOperand - 1
  19.             Natural divisible(*firstOperand, k, firstOperandLen);
  20.             while(COM_NN_D().get(&divisible, secondOperand)->get(0) == 2){
  21.                 secondOperand = MUL_ND_N().get(secondOperand, &firstDigit);
  22.                 firstDigit.set(0, firstDigit.get(0) + 1);
  23.                 flag = true;
  24.             }
  25.             if(flag){ // found a firstDigit
  26.                 if(COM_NN_D().get(&divisible, secondOperand)->get(0) == 1) // confirming that we got an actual first digit
  27.                     firstDigit.set(0, firstDigit.get(0) - 1);
  28.                 break;
  29.             }
  30.         }
  31.     }
  32.     Natural k(std::vector<uint8_t>{k});
  33.     return new Natural(&MUL_Nk_N().get(&firstDigit, &k));
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement