dr.watkins9
dr.watkins9

Reputation: 11

How can I program the Rational Root Theorem in JavaScript?

I am trying to program the Rational Root Theorem into a calculator on my website. For those who don't know what the Rational Root Theorem is, here is a link to it. So far I have the following:

<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>

But when I press the "GO!" button, nothing happens at all. I'm sure it's a stupid little simple mistake I made somewhere that I'm not catching. I was hoping someone could help me figure out the issue with that, and I was also wondering if there was any way I could condense the code with a for loop when it's repeating the "finding the factors", "testing the factors", "depreciating the polynomial", and "reseting the factor list" part. And if anyone has any other helpful tips, I'm open for everything. I'm not a professional programmer. I would love any advice you can give. Sorry the code section is so long, it's very repetitive. I tried to mimic the process that people go through when they're solving the problem on paper. After this is worked out, my next goal is to make it show its work so that it practically looks like a person doing it. Thanks for your help!

(I also wasn't completely sure about the for-of loops, it was something new that I saw on a website and I was testing it out. This is my first time using it. I was assuming it was basically the same thing as a PHP foreach loop. So let me know if I'm messing up with that.)

Upvotes: 1

Views: 241

Answers (1)

aset rahmgaliev
aset rahmgaliev

Reputation: 1

//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);
        //in this case the possible zero is not taken into account
        if (res.arr && res.arr.length === 2) {
            flag = false;
            results.push( res.arr.pop()/res.arr[0] );
        }
        
        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 );

Upvotes: 0

Related Questions