# Polynomials Division Script

#### MATLAB/OCTAVE Script to Perform Polynomial Division

In Modular Arithmetic we perform also addition, subtraction and multiplication. Modular division
is avoided. Modular addition means finding Modulo of two or more added numbers. Modular
subtraction means to find Modulo of two or more subtracted numbers. Modular Multiplication
means finding Modulo of two or more multiplied numbers. Modular division doesn’t exist as it
is ignored because we need to guarantee that the quotient (of divided numbers before Modulo
operation) is an integer.

Division operation is still need for some arithmetics like CRC, therefore to perform such operation, the (dividend & divisor) are considered as polynomials.
The following script can be used to perform the division and it can run in MATLAB or Octave.

You can clone it directly from this Github page:
https://github.com/electgon/MATLAB_scripts/blob/master/crc_division.m

```clear

dividend = [1,1,0,1,0,1,1,0,1,1,0,0,0,0];
divisor =[1,0,0,1,1];

len_end = length(dividend);
len_sor = length(divisor);

if (len_sor <= len_end)
len_diff = len_end - len_sor;
for app_idx = len_sor+1 : len_sor+len_diff
divisor(:, app_idx) = 0;
end
else
printf("Division is not possible, dividend must be longer than divisor\n")
end

if (len_diff >= 0)
for div_idx = 1:len_diff+1

if (dividend(div_idx) == 1)
dividend = xor(dividend, divisor);
div_res(div_idx) = 1;
else
div_res(div_idx) = 0;
end
if (div_idx == len_diff+1)
break;
end
divisor = shift(divisor, 1);
divisor(:,1) = 0;

end
end

quotient  = dec2hex(bi2de(div_res, 'left-msb'))
remainder = dec2hex(bi2de(dividend, 'left-msb'))
divisor = dec2hex(bi2de(divisor, 'left-msb'))
```