re: Prime numbers VIEW POST


Easy improvement would be skipping even numbers

for (let divisor = 3; divisor <= square; divisor += 2) {
  if (number % divisor === 0) {
    return false

You make a good point, I hadn’t considered that actually!

If I was to add this change it would just replace the conditional at the beginning of the function body which checks if the number is even and greater than 2 so this shouldn’t ever execute unless it’s an odd number that is passed in as input, in which case this becomes a good performance improvement, especially for larger numbers since odd numbers aren’t divisible by even ones without a remainder being present of course.

Thanks for commenting! 👍


It doesn't replace the conditional which checks if the number is even. That condition is still important.

Prime numbers are not even (except 2), so we don't have to check if they are divisible by even number.

Exactly, if you check the article, I added as much to the implementation section and tagged you with the kudos 😉

Nice! Thanks for tagging, it wasn't really needed. I just wanted to improve solution.

I always give credit where credits due so all good and thanks again for the comment 🙂👍.

Code of Conduct Report abuse