-1

I have written the below code to check if a number is prime, and it works correctly. But to my amazement when it pass 25 as a value, it returns "true" instead of "false" because 25 is not a prime number.

So I decided to share. Please, what I am doing wrong here?

function isPrime(number) {
   return number % 2 !== 0 && number % 3 !== 0 ? true : false;
}

isPrime(4) returns false;
isPrime(23) returns true;
isPrime(25) returns true; 

"(Here is where I got alarmed. 25 Should return false too)

2 Answers2

2

Your condition is very minimal. What if the number is not divisible by 2 or 3 but divisible by 5 or 7? Besides how come isPrime(23) returns false is okay? 23 is a prime as well it should return true. What you need to do is iterate through all the number from n-1 to 2(here n is the number you are checking) and check if any of the number divides n without a remainder. You can do the following,

function isPrime(number) {
    let isPrimeNum = true;
    for(let i = number-1; i>=2; i--) {
      if(number%i === 0) isPrimeNum = false;
    }
    return isPrimeNum;
}

console.log(isPrime(23));
console.log(isPrime(25));

There is a lot of way you can optimize the above solution. I kept it as a challenge for you to find out and do by yourself. You can start from here.

Md Sabbir Alam
  • 4,937
  • 3
  • 15
  • 30
1

Your code only checks if the input is divisible by 2 or 3. However, a prime number (by definition) is a number that's only divisible by 1 and itself.

Therefore, you have to check every number less than or equal to sqrt(n). (It's enough to scan this area, as if there's a divisor greater than that, it must have a pair that falls in that range.

The loop iterates upwards, so it can early-return, if the number is divisible by a small prime.

function isPrime(number){
  if(number <= 1 || number !== Math.floor(number))
    return false
  const sqrtNumber = Math.sqrt(number)
  for(let n = 2; n <= sqrtNumber; n++)
    if(!(number % n))
      return false
  return true
}


console.log(isPrime(-42)) //false
console.log(isPrime(1)) //false
console.log(isPrime(3.14)) //false
console.log(isPrime(4)) //false
console.log(isPrime(23)) //true
console.log(isPrime(25)) //false
FZs
  • 16,581
  • 13
  • 41
  • 50
  • Thanks for sharing. I am very much aware what a prime number is. I just needed to check for a small range say between 0 through 100. Your solution is helpful, but what about 0 and 1? – louismarkin Feb 17 '21 at 07:31
  • @louismarkin If your number is guaranteed to be under 100, you'd have to check (by the same reasoning) every number up to 10 manually, to be sure. However this code always loops the ideal amount of times, and will work with any number. – FZs Feb 17 '21 at 07:38
  • @louismarkin Good point about 0 and 1, added a check for them; and also for non-integers – FZs Feb 17 '21 at 07:40
  • Actually, you are testing for multiple even numbers. You should check if it is divisible by 2 and then check the divisibility by only odd numbers. – Bruno Canettieri Feb 17 '21 at 08:06
  • @BrunoCanettieri Now, it goes upwards, and the early return takes care of that. Thanks for pointing out! – FZs Feb 17 '21 at 08:20
  • sure, but you are still checking for multiple even numbers though. – Bruno Canettieri Feb 17 '21 at 08:38
  • @BrunoCanettieri Yes, it would be possible to cancel checking the multiples of every prime number found along the way; to do that, however, you'd need to keep track of them, and finally end up with a more complex and maybe not faster than the original code (that's called *premature optimisation*). – FZs Feb 17 '21 at 09:26
  • @FZs Sorry, I really don't think this case classify as premature optimization (but whatever, for a max int of 100 it probably wont make a difference). I don't think I can format as code in comments but it would be like if(!(number % 2)) return number === 2 and for(let n = 3; n <= sqrtNumber; n+=2) – Bruno Canettieri Feb 17 '21 at 15:51