Home > Web Front-end > JS Tutorial > body text

JS implements multiplication of two large numbers (integers)_javascript skills

WBOY
Release: 2016-05-16 16:50:55
Original
2148 people have browsed it

Large numbers, that is, numbers that exceed the maximum range of numbers that can be represented by language. At this time, the numbers can only be represented by strings. So how to multiply two large numbers? I implemented it with JS, the code is as follows:

Copy code The code is as follows:

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("");
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template