dongnguyenltqb
5/12/2018 - 5:38 PM

## BIGNUM.js

``````function tong(a,b){
while(a.length<b.length) a="0"+a;
while (a.length >b.length) b = "0" + b;
a="0"+a;
b="0"+b;
var du=0;
var c="";
for(var i=a.length-1;i>=0;i--){
c=((Number(a[i])+Number(b[i])+du)%10).toString()+c;
du = Math.floor((Number(a[i]) + Number(b[i]) + du) / 10);
}
while(c[0]==="0") c=c.substring(1);
return c;
}
function hieu(a,b){
while (a.length > b.length) b = "0" + b;
while (a.length < b.length) a = "0" + a;
a = "0" + a;
b = "0" + b;
var c="";
if(a<b){ var t=a; a=b;b=t;}
var no=0;
for(var i=a.length-1;i>=0;i--)
{
if((Number(a[i])-no)<Number(b[i])){
c = (Number(a[i]) - no + 10 - Number(b[i])).toString()+c;
no = 1;
} else {
c = (Number(a[i]) - no - Number(b[i])).toString() + c;
no=0;
}
}
while (c[0] === "0") c = c.substring(1);
if(c==="") c=0;
return c;
}
function tich(a,b){
var p=[];
while (a.length > b.length) b = "0" + b;
while (a.length < b.length) a = "0" + a;
if (a < b) { var t = a; a = b; b = t; }
a = "0" + a; m=a.length;
b = "0" + b; n=b.length;
for(var i=n-1;i>=0;i--){
var c="";
var nho=0;
for(var j=m-1;j>=0;j--){
c=((Number(a[j])*Number(b[i])+nho)%10).toString()+c;
nho=Math.floor((Number(a[j])*Number(b[i])+nho)/10);
}
p[n-i-1]=c;
}
dem="0";
for(var i=1;i<n;i++){
p[i]=p[i]+dem;
dem=dem+"0";
}
var result="0";
for(var i=0;i<n;i++) result=tong(result,p[i]);
return result;
}
var a = "13456"; var b = "986";
console.log("tong la ",tong(a, b));
console.log("hieu la ",hieu(a,b));
console.log("tich la ",tich(a,b));``````