我正在尝试将有理根定理编程到我网站上的计算器中。对于那些不知道什么是有理根定理的人,这里有一个链接。到目前为止,我有以下内容:
<input type="number" id="5" style="width:50px;" value=0>x<sup>5</sup>+<input type="number" id="4" style="width:50px;" value=0>x<sup>4</sup>+<input type="number" id="3" style="width:50px;" value=0>x<sup>3</sup>+<input type="number" id="2" style="width:50px;" value=0>x<sup>2</sup>+<input type="number" id="1" style="width:50px;" value=0>x+<input type="number" id="0" style="width:50px;" value=0>
<button onclick="find()">GO!</button>
<script>
function find(){
var roots = new Array();
var root5,root4,root3,root2,root1 = 0;
var afac = new Array();
var zfac = new Array();
var for1, for2;
var co5 = document.getElementById('5').value;
var co4 = document.getElementById('4').value;
var co3 = document.getElementById('3').value;
var co2 = document.getElementById('2').value;
var co1 = document.getElementById('1').value;
var co0 = document.getElementById('0').value;
//find the factors of the first coefficient
for (i=1;i<=co5;i++){
if (co5 % i == 0){
afac[afac.length] = 0-i;
afac[afac.length] = i;
}
}
//find the factors of the last coefficient
for (i=1;i<=co0;i++){
if (co0 % i == 0){
zfac[zfac.length] = 0-i;
zfac[zfac.length] = i;
}
}
//test all the factors using the Rational Root Theorem
for (for1 of afac){
for (for2 of zfac){
var x = for2/for1;
var answer = (co5(x)^5)+(co4(x)^4)+(co3(x)^3)+(co2(x)^2)+(co1*x)+co0;
if (answer == 0){
root5 = x;
}
}
}
//depreciate the polynomial
co4 = co4 + (root5 * co5);
co3 = co3 + (root5 * co4);
co2 = co2 + (root5 * co3);
co1 = co1 + (root5 * co2);
co0 = co0 + (root4 * co1);
//reset the factor lists
afac = new Array();
zfac = new Array();
//find the factors of the first coefficient
for (i=1;i<=co4;i++){
if (co4 % i == 0){
afac[afac.length] = 0-i;
afac[afac.length] = i;
}
}
//find the factors of the last coefficient
for (i=1;i<=co0;i++){
if (co0 % i == 0){
zfac[zfac.length] = 0-i;
zfac[zfac.length] = i;
}
}
//test all the factors using the Rational Root Theorem
for (for1 of afac){
for (for2 of zfac){
var x = for2/for1;
var answer = (co4(x)^4)+(co3(x)^3)+(co2(x)^2)+(co1*x)+co0;
if (answer == 0){
root4 = x;
}
}
}
//depreciate the polynomial
co3 = co3 + (root4 * co4);
co2 = co2 + (root4 * co3);
co1 = co1 + (root4 * co2);
co0 = co0 + (root4 * co1);
//reset the factor lists
afac = new Array();
zfac = new Array();
//find the factors of the first coefficient
for (i=1;i<=co3;i++){
if (co3 % i == 0){
afac[afac.length] = 0-i;
afac[afac.length] = i;
}
}
//find the factors of the last coefficient
for (i=1;i<=co0;i++){
if (co0 % i == 0){
zfac[zfac.length] = 0-i;
zfac[zfac.length] = i;
}
}
//test all the factors using the Rational Root Theorem
for (for1 of afac){
for (for2 of zfac){
var x = for2/for1;
var answer = (co3(x)^3)+(co2(x)^2)+(co1*x)+co0;
if (answer == 0){
root3 = x;
}
}
}
//depreciate the polynomial
co2 = co2 + (root3 * co3);
co1 = co1 + (root3 * co2);
co0 = co0 + (root3 * co1);
//quadratic formula
root2 = ((0 - co1) - Math.sqrt((co1^2) - (4 * co2 * co0))) / (2 * co3);
root1 = ((0 - co1) + Math.sqrt((co1^2) - (4 * co2 * co0))) / (2 * co3);
document.getElementById('root1').innerHTML = root1;
document.getElementById('root2').innerHTML = root2;
document.getElementById('root3').innerHTML = root3;
document.getElementById('root4').innerHTML = root4;
document.getElementById('root5').innerHTML = root5;
alert('hello');
}
</script>
<br>Roots:<br>
<span id="root1"></span><br>
<span id="root2"></span><br>
<span id="root3"></span><br>
<span id="root4"></span><br>
<span id="root5"></span>
但是当我按下“GO!”时按钮,什么也没有发生。我确信这是我在某个地方犯的一个愚蠢的小错误,但我没有发现。我希望有人能帮助我解决这个问题,而且我也想知道是否有任何方法可以在重复“查找因素”、“测试因素”、“时用 for 循环压缩代码”折旧多项式”和“重置因子列表”部分。如果有人有任何其他有用的建议,我愿意接受一切。我不是专业程序员。我希望您能提供任何建议。抱歉,代码部分太长,非常重复。我试图模仿人们在纸上解决问题时所经历的过程。解决这个问题后,我的下一个目标是让它展示它的工作原理,使它看起来像一个人在做它。感谢您的帮助!
(我也不完全确定 for-of 循环,这是我在网站上看到的新东西,我正在测试它。这是我第一次使用它。我假设它基本上是相同的东西作为 PHP foreach 循环。如果我搞砸了,请告诉我。)
//Much has not been taken into account, but there is a beginning:
function Factors(n) {
const factors = [1];
if (n < 2) return factors;
var length = n/2;
for (let i = 2; i <= length; i++) {
if (n % i === 0) factors.push(i);
}
factors.push(n)
return factors;
}
//constant - number, coefficient - number
function rationalRootTest (constant, coefficient) {
var dividends = Factors(constant);
var divisors = Factors(coefficient);
var quotients = [];
dividends.forEach(dividend => {
divisors.forEach(divisor => {
quotients.push(dividend/divisor);
quotients.push(dividend/divisor*-1);
});
});
return quotients;
}
function syntheticIteration (coefficients, divisors) {
var results = [];
for (let i = 0; i < divisors.length; i++) {
var divisor = divisors[i];
results.push(coefficients[0]);
for (let j = 1; j < coefficients.length; j++) {
let value = coefficients[j] + ( results.slice(-1)[0]*divisor);
results.push(value);
}
if (results.pop() === 0) {
return {val: divisor, arr: results};
}
results = [];
}
return false;
}
//coefficients - number[]
function syntheticDivision (coefficients) {
var listOfPossibleSolutions = rationalRootTest( coefficients.slice(-1)[0], coefficients[0] );
var flag = true;
var results = [];
while (flag) {
var res = syntheticIteration(coefficients, listOfPossibleSolutions);
if (!res) flag = false;
if (res.val) results.push(res.val);
if (res.arr && res.arr.length === 2) {
flag = false;
results.push( ...res.arr );
}
coefficients = res.arr;
}
return results;
}
//console.log(Factors(16));
//console.log(Factors(15));
//console.log(rationalRootTest(16, 15));
// console.log( syntheticIteration([15, 1, -52, 20, 16], [1,-1, 0.3333333333333333,-0.3333333333333333,0.2, -0.2,0.06666666666666667,-0.06666666666666667,2,-2,0.6666666666666666,-0.6666666666666666,0.4,-0.4,0.13333333333333333,-0.13333333333333333,4,4,
// 1.3333333333333333, -1.3333333333333333, 0.8,
// -0.8, 0.26666666666666666, -0.26666666666666666,
// 8, -8, 2.6666666666666665,
// -2.6666666666666665, 1.6, -1.6,
// 0.5333333333333333, -0.5333333333333333, 16,
// -16, 5.333333333333333, -5.333333333333333,
// 3.2, -3.2, 1.0666666666666667,
// -1.0666666666666667
// ]) );
let arr = [15, 1, -52, 20, 16];
console.log( syntheticDivision(arr) );
//arr.pop();
//console.log( arr );