So I’m trying to input a number as an argument of my function and return the number of 0-1 fractions that can not be reduced at all (code wars problem).
I kind of already did it but my code keeps timing out for larger numbers.
How could I make it more efficient?
This is my code:
function properFractions(n) {
const numerators = Array.from({ length: n }, (_, i) => i + 1).slice(0, -1);
const oneToNine = Array.from({ length: 8 }, (_, i) => i + 2);
let count = n;
if (numerators.length === 0) count = 0;
else if (numerators.length < 2) {
count--;
} else if (numerators.length > 1) {
for (let y = 0; y <= numerators.length; y++) {
if (n % numerators[y] === 0) count--;
for (let i = 0; i < oneToNine.length; i++) {
if (numerators[y] < oneToNine[i] && n < oneToNine[i]) break;
else if (numerators[y] > oneToNine[i] && n > oneToNine[i]) {
if (numerators[y] % oneToNine[i] === 0 && n % oneToNine[i] === 0)
count--;
}
}
}
}
return count;
}
^ can’t handle bigger numbers without timing out