// the function returns the multiply of p1 and p2 big
function mul_b(p1, p2) {
var eql = ""; // fine value
var r_eql = ""; // reversed value retured
var carry = 0;
var length_p1 = p1.length - 1;
console.log( "length first number " + length_p1);
var length_p2 = p2.length - 1;
console.log( "length secnd number " + length_p2);
if (length_p1 != length_p2){// eql two number by padding zeros
return "Please make both sides equal!";
}
console.log(length_p1 + "=" + length_p2);
var A1,A2;
var A3 = 0;
for(var j = length_p1; j>=0; j--){
var k = j;
A3 = 0;
for( var i = length_p1 ; i >= j ; i-- ){
A1 = parseInt(p1.charAt(k));
A2 = parseInt(p2.charAt(i));
k+=1;
console.log("("+A1+","+A2+")");
A3 = (A1 * A2) + A3;
console.log("("+A3+")");
}
eql = A3.toString().concat(eql);
console.log("("+eql+")");
}
for(var j = (length_p1-1); j>=0; j--){
var k = j;
A3 = 0;
for( var i = 0; i<=j; i++ ){
A1 = parseInt(p1.charAt(k));
A2 = parseInt(p2.charAt(i));
k-=1;
console.log("("+A1+","+A2+") " + k + " , "+ i);
A3 = (A1 * A2)+A3;
console.log("("+A3+")");
}
eql = A3.toString().concat(eql);
console.log("("+eql+")");
}
var f_eql = "";
f_eql = eql.charAt(0);
for (var i = 1; i<=(eql.length); i++){
if (eql.charAt(i+1) == ""){
f_eql += (parseInt(eql.charAt(i)) + carry + 0).toString();
}else{
if ((parseInt(eql.charAt(i)) + parseInt(eql.charAt(i+1)) + carry)<10){//carry
f_eql += (parseInt(eql.charAt(i)) + parseInt(eql.charAt(i+1))+ carry).toString();
carry = 0;
} else {
f_eql += (parseInt(eql.charAt(i)) + parseInt(eql.charAt(i+1)) - 10).toString();
carry = 1;
}
}
console.log("("+eql.charAt(i)+")"+eql.charAt(i+1));
i += 1;
}
console.log("fine = ("+eql+")");
console.log("("+f_eql+")");
return f_eql;
}
拜托,我得到错误携带并让我头痛,我的错误在哪里?并且这个代码很酷或者是什么它的新方法将两个大数字乘以它称为三角乘法。
当我尝试乘法时
123456789012 --- 12位数
141020355684120165200226244249240216176119445036144
我的方法是
放下第一个数字 第二个加到第三个并放下 而第四次加入到第五名并放下 等等到最后的数字
但我在携带时遗漏了错误 那么请帮助我?
答案 0 :(得分:0)
我做了一个互动的例子
$(function() {
var update = function() {
var itemA = $("#nrA").val();
var itemB = $("#nrB").val();
var start = new Date().getTime();
var result = new BigNumber(itemA).multiply(itemB.toString())
var end = new Date().getTime();
var total = end - start;
$("#fact").val(result);
$("#time").val(total + " ms");
};
$("#nrA").on("input", function() {
update();
});
$("#nrB").on("input", function() {
update();
});
update();
});
#fact {
width: 100%;
height: 40px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script src="http://yourjavascript.com/61473108321/bignumber.js"></script>
<label for="nrA">Number A:</label>
<input id="nrA" value="100"></input>
<label for="nrB">Number B:</label>
<input id="nrB" value="100"></input><br/>
<label for="fact">A * B:</label><br/>
<textarea id="fact"></textarea><br/>
<label for="time">Computed in:</label>
<input id="time"></input>