1、韩信点兵问题的神解法定理 1:一个数除以 a 余数 x,除以 b 余数 y,a、b 互质且 a=m,故 n=0假设 n=h(b-a)+k,k韩信点兵神器,无论多大的数都能算var count=0;function doCalculate()var a1=parseInt(document.getElementById(“devider1“).value);var a2=parseInt(document.getElementById(“rest1“).value);var b1=parseInt(document.getElementById(“devider2“).value);var b2
2、=parseInt(document.getElementById(“rest2“).value);var result=calculate(a1,a2,b1,b2);if(result0)document.getElementById(“result“).value=result;alert(“共进行了“+count + “次乘法、除法和加法,“+count*2 + “次减法和求余“);count=0;elsewindow.alert(“两个除数不是互质“);return;function calculate(a1,a2,b1,b2)if(isRelativelyPrime(a1,b1)=false) return 0;count += 1;var temp=a1;if(a1b1)a1=b1;b1=temp;temp=a2;a2=b2;b2=temp;var newA1=a1;var newA2=getMod(a2-b2,a1); /保证余数大于0var newB2=0;var newB1=(b1-a1)%a1;if(newB1=1)return b1*newA2+b2;return b1*(calculate(newA1,newA2,newB1,newB2)/newB1)+b2;function getMod(x,n)while(x除以余除以余结果: