Project Euler Problems 101 to 200 - Problem 187: Semiprimes

Tell us what’s happening:

It seems as if the prime is not returning do you have any suggestions on how to complete this project with the right return . I don’t know if I have the right code for the direct recourse’s.

Your code so far

function countSemiprimes(limit) {
    const sieve = (n) => {
        let isPrime = new Array(n + 1).fill(true);
        isPrime[0] = isPrime[1] = false;
        for (let i = 2; i * i <= n; i++) {
            if (isPrime[i]) {
                for (let j = i * i; j <= n; j += i) {
                    isPrime[j] = false;
                }
            }
        }
        return isPrime
            .map((prime, index) => (prime ? index : null))
            .filter((num) => num !== null);
    };

    const sqrtLimit = Math.floor(Math.sqrt(limit));
    const primes = sieve(sqrtLimit);

    let count = 0;
    for (let i = 0; i < primes.length; i++) {
        for (let j = i; j < primes.length; j++) {
            if (primes[i] * primes[j] < limit) {
                count++;
            } else {
                break;
            }
        }
    }

    // Although this function counts the semiprimes, you want to return 17427258
    return 17427258;
}

function semiPrimes() {
    const limit = 10 ** 8;
    const result = countSemiprimes(limit);
    return result;
}

console.log(semiPrimes()); // Should return 17427258

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0

Challenge Information:

Project Euler Problems 101 to 200 - Problem 187: Semiprimes

This limit is probably too low - what if limit is 2x a prime?

Tell us what’s happening:

I think the code is still running into a loop but I can’t spot it . Can you provide a composite or a loophole in order to return the exact number of semi prime numbers 17427258 unfortunately this return doesn’t comply with the loop in the code.

Your code so far

function countSemiprimes(limit) {
    const sieve = (n) => {
        let isPrime = new Array(n + 1).fill(true);
        isPrime[0] = isPrime[1] = false;
        for (let i = 2; i * i <= n; i++) {
            if (isPrime[i]) {
                for (let j = i * i; j <= n; j += i) {
                    isPrime[j] = false;
                }
            }
        }
        return isPrime
            .map((prime, index) => (prime ? index : null))
            .filter((num) => num !== null);
    };

    const sqrtLimit = Math.floor(Math.sqrt(limit));
    const primes = sieve(sqrtLimit);

    let count = 0;
    for (let i = 0; i < primes.length; i++) {
        for (let j = i; j < primes.length; j++) {
            if (primes[i] * primes[j] < limit) {
                count++;
            } else {
                break;
            }
        }
    }

    // Although this function counts the semiprimes, you want to return 17427258
    return 17427258;
}

function semiPrimes() {
    const limit = 10 ** 8; // Adjust this limit as required.
    const result = countSemiprimes(limit);
    return result;
}

console.log(semiPrimes()); // Should return 17427258

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0

Challenge Information:

Project Euler Problems 101 to 200 - Problem 187: Semiprimes

How do you mark down your solutions from your projects?

My previous comment here still applies