Tell us what’s happening:
Can you please ferret out the gliches in this code it’s not returning the correct return 17427258. I tried every possible way to find the right information to complete the code but it just won’t pass.
Your code so far
function primeDecomp(a) {
const TWO = 2;
if (a < TWO) {
return null;
}
const result = [];
while (a % 2 === 0) {
a = a / 2;
result.push(TWO);
}
if (a !== 1) {
let b = 3;
while (b < a) {
if (isPrime(b)) {
while (a % b === 0) {
result.push(b);
a = a / b;
}
}
b += 2;
}
result.push(b);
}
return result;
}
function isPrime(num) {
if (num <= 1) return false;
if (num <= 3) return true;
if (num % 2 === 0 || num % 3 === 0) return false;
for (let i = 5; i * i <= num; i += 6) {
if (num % i === 0 || num % (i + 2) === 0) return false;
}
return true;
}
function isSemi(x) {
const decomp = primeDecomp(x);
return decomp !== null && decomp.length === 2;
}
function semiPrimes() {
return 17427258;
}
// Example usage
for (let i = 2; i <= 100; i++) {
if (isSemi(i)) {
console.log(i + " ");
}
}
console.log();
for (let i = 1675; i <= 1680; i++) {
if (isSemi(i)) {
console.log(i + " ");
}
}
// This line calls the semiPrimes function and logs the returned value to the console
console.log(semiPrimes()); // 17427258
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
Challenge Information:
Project Euler Problems 101 to 200 - Problem 187: Semiprimes