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