相思资源网 Design By www.200059.com
大数,即超出语言所能表示的数字最大范围的数字,此时的数字只能用字符串来表示。那么如何实现两个大数相乘呢?我用JS实现了一下,代码如下:
复制代码 代码如下:
console.log(bigMut("567", "1234")); // 699678
function bigMut(big, common) {
big += "";
common += "";
if (big.length < common.length) {
big = [common, common = big][0];
}
big = big.split("").reverse();
var oneMutManyRes = [];
var i = 0,
len = big.length;
for (; i < len; i++) {
oneMutManyRes[oneMutManyRes.length] = oneMutMany(big[i], common) + getLenZero(i);
}
var result = oneMutManyRes[0];
for (i = 1, len = oneMutManyRes.length; i < len; 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 <= len; 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 <= maxLen; 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 < common.length) {
big = [common, common = big][0];
}
big = big.split("").reverse();
var oneMutManyRes = [];
var i = 0,
len = big.length;
for (; i < len; i++) {
oneMutManyRes[oneMutManyRes.length] = oneMutMany(big[i], common) + getLenZero(i);
}
var result = oneMutManyRes[0];
for (i = 1, len = oneMutManyRes.length; i < len; 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 <= len; 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 <= maxLen; 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("");
}
标签:
整数,相乘
相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com
暂无JS实现两个大数(整数)相乘的评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。