Answers for "Write a recursive function isPrime that determines if a number is prime and returns true if it is. recursion js"

0

Write a recursive function isPrime that determines if a number is prime and returns true if it is. recursion js

function isPrime(num, div = 3){
      if(num === 2) return true;
      if(num < 2 || num % 2 === 0)  return false;
      if(div * div > num) return true;
      if(num % div === 0) return false;
      return isPrime(num, div + 2);
}
console.log(isPrime(1)); //-> false
console.log(isPrime(2)); //-> true
console.log(isPrime(3)); //-> true
console.log(isPrime(4)); //-> false
Posted by: Guest on March-22-2021

Code answers related to "Write a recursive function isPrime that determines if a number is prime and returns true if it is. recursion js"

Code answers related to "Javascript"

Browse Popular Code Answers by Language