JS实现两个大数(整数)相乘_javascript技巧
程序员文章站
2022-05-03 08:50:46
...
大数,即超出语言所能表示的数字最大范围的数字,此时的数字只能用字符串来表示。那么如何实现两个大数相乘呢?我用JS实现了一下,代码如下:
console.log(bigMut("567", "1234")); // 699678
function bigMut(big, common) {
big += "";
common += "";
if (big.length big = [common, common = big][0];
}
big = big.split("").reverse();
var oneMutManyRes = [];
var i = 0,
len = big.length;
for (; i oneMutManyRes[oneMutManyRes.length] = oneMutMany(big[i], common) + getLenZero(i);
}
var result = oneMutManyRes[0];
for (i = 1, len = oneMutManyRes.length; i result = bigNumAdd(result, oneMutManyRes[i]);
}
return result;
}
function getLenZero(len) {
len += 1;
var ary = [];
ary.length = len;
return ary.join("0");
}
function oneMutMany(one, many) {
one += "";
many += "";
if (one.length != 1) {
one = [many, many = one][0];
}
one = parseInt(one, 10);
var i = 0,
len = many.length,
resAry = [],
addTo = 0,
curItem,
curRes,
toSave;
many = many.split("").reverse();
for (; i curItem = parseInt(many[i] || 0, 10);
curRes = curItem * one + addTo;
toSave = curRes % 10;
addTo = (curRes - curRes % 10) / 10;
resAry.unshift(toSave);
}
if (resAry[0] == 0) {
resAry.splice(0, 1);
}
return resAry.join("");
}
function bigNumAdd(big, common) {
big += "";
common += "";
var maxLen = Math.max(big.length, common.length),
bAry = big.split("").reverse(),
cAry = common.split("").reverse(),
i = 0,
addToNext = 0,
resAry = [],
fn,
sn,
sum;
for (; i fn = parseInt(bAry[i] || 0);
sn = parseInt(cAry[i] || 0);
sum = fn + sn + addToNext;
addToNext = (sum - sum % 10) / 10;
resAry.unshift(sum % 10);
}
if (resAry[0] == 0) {
resAry.splice(0, 1);
}
return resAry.join("");
}
复制代码 代码如下:
console.log(bigMut("567", "1234")); // 699678
function bigMut(big, common) {
big += "";
common += "";
if (big.length big = [common, common = big][0];
}
big = big.split("").reverse();
var oneMutManyRes = [];
var i = 0,
len = big.length;
for (; i oneMutManyRes[oneMutManyRes.length] = oneMutMany(big[i], common) + getLenZero(i);
}
var result = oneMutManyRes[0];
for (i = 1, len = oneMutManyRes.length; i result = bigNumAdd(result, oneMutManyRes[i]);
}
return result;
}
function getLenZero(len) {
len += 1;
var ary = [];
ary.length = len;
return ary.join("0");
}
function oneMutMany(one, many) {
one += "";
many += "";
if (one.length != 1) {
one = [many, many = one][0];
}
one = parseInt(one, 10);
var i = 0,
len = many.length,
resAry = [],
addTo = 0,
curItem,
curRes,
toSave;
many = many.split("").reverse();
for (; i curItem = parseInt(many[i] || 0, 10);
curRes = curItem * one + addTo;
toSave = curRes % 10;
addTo = (curRes - curRes % 10) / 10;
resAry.unshift(toSave);
}
if (resAry[0] == 0) {
resAry.splice(0, 1);
}
return resAry.join("");
}
function bigNumAdd(big, common) {
big += "";
common += "";
var maxLen = Math.max(big.length, common.length),
bAry = big.split("").reverse(),
cAry = common.split("").reverse(),
i = 0,
addToNext = 0,
resAry = [],
fn,
sn,
sum;
for (; i fn = parseInt(bAry[i] || 0);
sn = parseInt(cAry[i] || 0);
sum = fn + sn + addToNext;
addToNext = (sum - sum % 10) / 10;
resAry.unshift(sum % 10);
}
if (resAry[0] == 0) {
resAry.splice(0, 1);
}
return resAry.join("");
}
上一篇: Drupal网站的SEO功能_PHP教程
下一篇: Python和Django安装步骤
推荐阅读
-
两个DIV等高的JS的实现代码_javascript技巧
-
js实现两个值相加alert出来精确到指定位_javascript技巧
-
JS实现两个大数(整数)相乘_javascript技巧
-
JS实现合并两个数组并去除重复项只留一个的方法_javascript技巧
-
JS实现横向与竖向两个选项卡Tab联动的方法_javascript技巧
-
JS实现的用来对比两个用指定分隔符分割的字符串是否相同_javascript技巧
-
js实现一个链接打开两个链接地址的方法_javascript技巧
-
JavaScript js 实现两个小数的相乘、相除功能
-
js实现两个值相加alert出来精确到指定位_javascript技巧
-
JS响应鼠标点击实现两个滑块区间拖动效果_javascript技巧