0

This is as far as I've gotten. I have factored the numbers successfully, but I just need to distribute and divide them into the pOverQ array.

var p = readInt("What is your p: ");
var q = readInt("What is your q: ");
var factorP = [];
var factorQ = [];
var pOverQ = [];
var num = 0;
    


function start(){
    factor(p, q);
    println(factorP);
    println(factorQ);
    p_Q(p, q);
    println(pOverQ);
}

function factor(p ,q){

    for(var i = 1; i <= p; i++) {
        
        if(p % i == 0){
             factorP.push(i);
        }
        if(q % i == 0){
            factorQ.push(i);
        }
    }
}

function p_Q(p, q){
    for(var i = 0; i < (factorP.length)*2; i++){
        pOverQ.push(factorP[i]/factorQ[i]);
    }
}

I hope you can help!!!

  • can you add input and expected output? – Rahul Kumar Jan 28 '22 at 04:58
  • What does the "p" in the code mean? – A1exandr Belan Jan 28 '22 at 05:18
  • The function `readInt()` is defined where? `println()` is Java. – zer00ne Jan 29 '22 at 05:42
  • Assuming that `p` and `q` are the factors of the constant-term and the leading co-efficient, respectively, may we know why the loop `i = 1; i <= p; i++` in `factor(p, q)` only accounts for integers from 1 to p for both `p` and `q`. Should it be two separate loops, 1 for `p` and another for `q`. – jsN00b Jan 29 '22 at 05:42
  • Further, as per rational-zeroes-theorem, the factors need to be considered both positive and negative. In this case, only positive integers are being considered. And, finally: in the function `p_Q`, there is only one loop which ties `p0` with `q0`, `p1` with `q1`, so on & so forth. However, one needs to consider all possibilities such as `p0` with `q1`, `p0` with `q2`, etc; thus, this requires iterating over both the arrays. – jsN00b Jan 29 '22 at 05:45

1 Answers1

0

It looks like you are trying to build something like this: rational zeros theorem calculator

Here are a few steps towards it:

function factor(p){ // find all factors of a single number
  let res=[];
  for(let i=1;i<=p;i++) if(p%i == 0) res.push(i);
  return res;
}
function p_q(P,Q){ // combine P- and Q-arrays and find all distinct fractions
  const coll={}, qarr=factor(Q);
  factor(P).forEach(p=>qarr.forEach(q=>{
   let frac=reduce(p,q);
   coll[frac.join("/")]=frac
  }));
  return coll;
}
// https://stackoverflow.com/a/4652513/2610061
function reduce(num,den){ // reduce a given fraction
  // greatest common denominator:
  function gcd(a,b){return b ? gcd(b, a%b) : a;};
  const g = gcd(num,den);
  return [num/g, den/g];
}
const res=p_q(6,7);
console.log(Object.keys(res));  // show the fractions as text ...
console.log(Object.values(res).map(([n,d])=>n/d)); // ... and as values

As @jsN00b already mentioned, you will need to consider both, positive and negative values of the calculated fractions as possible solutions for your polynomium. But I 'll leave that bit to you.

Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43