Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DivInteger := proc(num1::list, num2::list)
- local d, n1, n2, u, j, q, r, m, n, w;
- if StripZeroes(num2) = [0] then error "Can't divide zero"; end if; # Prevent division by zero
- q := [];
- n := nops(num2);
- m := nops(num1) - n;
- d := floor(10/(op(1,num2) + 1)); # Get the normalization factor
- n1 := MultInteger(num1, [d]); # Normalize
- n2 := MultInteger(num2, [d]);
- u := [op(1..n + 1, n1)];
- for j from -(m) to -1 do
- if op(1, u) = op(1, n2) then
- q := [op(q), 9];
- else
- q := [op(q), floor((op(1, u)*10 + op(2, u)) / op(1, n2))];
- end if;
- w := MultInteger([op(-1, q)], n2);
- while CompInt(w, u) = 1 do
- q := subsop(-1 = (op(-1, q) - 1), q);
- w := SubInteger(w, n2);
- end do;
- r := SubInteger(u, w);
- if j < -1 then
- u := AddInteger(MultInteger(r, [1,0]), [op(j + 1, n1)]);
- end if
- end do;
- r := op(1, ShortDiv(r, d));
- return [q, r];
- end proc:
Add Comment
Please, Sign In to add comment