除法
其实就是借位相减。
a='157794750267131502212476817800345498121872783333389747424011531025366277535262539913701806290766479189477533597854989606803194253978660329941980786072432806427833685472618792592200595694346872951301770580765135349259590167490536138082469680638514416594216629258349130257685001248172188325316586707301643237607'
b='65'
x = a.split('').map(e => parseInt(e))
y = b.split('').map(e => parseInt(e))
div(x, y)
const div = (x, y) => {
const
q = []
let
cmp, times,
next = y.length,
r = x.slice(0, y.length)
do {
for (times = 0; times < 10; times++) {
/*
* 比较除数与余数大小
* 除数 > 余数 ? 1 : -1
*/
if (y.length != r.length)
cmp = y.length > r.length ? 1 : -1
else {
cmp = 0
for (let i = 0; i < r.length; i++) {
if (y[i] != r[i]) {
cmp = y[i] > r[i] ? 1 : -1
break
}
}
}
/*
* 余数大于除数
* 余数减去除数
*/
if (cmp < 0) {
const
delta = r.length - y.length
let
_i,
carry = 0
for (let i = y.length - 1; i >= 0; i--) {
_i = i + delta
r[_i] = r[_i] - y[i] + carry
r[_i] < 0 ? (r[_i] += 10, carry = -1) : carry = 0
}
carry && (r[_i-1] += carry)
for (; !r[0]; r.shift()) {}
} else break
}
q[next] = cmp ? times : (r = [x[next]], ++times)
cmp == 1 && r.push(x[next])
} while (next++ < x.length)
return q
}