Problem 5: Smallest multiple - can't pass the test cases

Tell us what’s happening:

My solution to this problem works in the console for all the test cases but it says that the last 2 cases (13 and 20) are incorrect. What’s going on?

Your code so far


function smallestMult(n) {
    let nums = [];
    for (let a = 1; a <= n; a++) {
        nums.push(a);
    }
    for (let a = n;; a++) {
        if (nums.every(elem => a % elem === 0)) {
            return a;
        }
    }
}

smallestMult(13);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/project-euler/problem-5-smallest-multiple

Same goes for “Project Euler: Problem 7: 10001st prime”
https://learn.freecodecamp.org/coding-interview-prep/project-euler/problem-7-10001st-prime
My solution works in the console but the last two test cases are incorrect according to FCC

function nthPrime(n) {
  let primes = [];
  let counter = 2;
  const isPrime = (num) => {
    for (let a = 2; a<num;a++) {
      if (num%a===0) {
        return false;
      }
    }
    return true;
  }
  while(primes.length<n) {
      if (isPrime(counter)) {
        primes.push(counter);
        counter = counter+1;
      }
      else counter = counter+1;
    }
  return primes[primes.length-1];
}

nthPrime(6);

Your code is not efficient enough. The FCC editor has protections that stop the code from running if it is needing too much time, and those are being triggered by your code

1 Like

While this is technically correct, this ‘solution’ is not conducive to a good learning experience. Students are spending time trying to incorrectly troubleshoot their logic while it is the coding environment that is to blame for their failures. There is no error to this effect and you cannot log the error. The solution to this isn’t “Your code is not efficient enough”. Coding efficiency is not a requirement of the exercise.

2 Likes

Even if that may be true you can’t pass the tests if your code is not efficient enough. You can bring this up in the GitHub repository if you want and if there is not an issue on the same topic (if there is one comment there)

Your “brute force” algorithm for determining primes is not efficient. Even with the brute force algorithm, you only need to go up to sqrt(num) when checking for primality, not num.

You should look up “Sieve of Erosthanes” for a much faster solution, though at the expense of more memory – the “1001th prime” solution will require a more clever algorithm than keeping the whole sieve in memory. It’s a tough one. Hint: it’s pretty likely the Miller-Rabin primality test will be accurate up to the 1001st prime, though I’m not 100% sure of that.