Find the prime number


Posted by Christy on 2023-01-20

Write a function named isPrime and pass a parameter n, if n is prime return true, else return false

function isPrime(n) {
  if (n === 1) return false;
  for (let i = 2; i < n; i++) {
    if (n % i === 0) {
      return false;
    }
  }
  return true;
}

function isPrime(n) {
  if (n === 1) return false;
  for (let i = 2; i < Math.sqrt(n); i++) {
    if (n % i === 0) {
      return false;
    }
  }
  return true;
}

console.log(isPrime(1));
console.log(isPrime(5));
console.log(isPrime(37));









Related Posts

Laravel Cors middleware

Laravel Cors middleware

React-[建立開發環境篇]-安裝vite & 將vite專案打包並部署

React-[建立開發環境篇]-安裝vite & 將vite專案打包並部署

關於 console.log 印出物件時所需注意的

關於 console.log 印出物件時所需注意的


Comments