print prime numbers in JS code example
Example 1: javascript find a prime number
function isPrime(num) {
if(num < 2) return false;
for (let k = 2; k < num; k++){
if( num % k == 0){
return false;
}
}
return true;
}
Example 2: prime numbers javascript
function isPrime(num) {
for (let i = 2; i * i <= num; i++)
if (num % i === 0)
return false;
return num > 1;
}
Example 3: javascript find prime number
function test_prime(n){
if (n===1) {
return false;
}else if(n === 2){
return true;
}else{
for(var x = 2; x < n; x++){
if(n % x === 0){
return false;
}
}
return true;
}
}
console.log(test_prime(37));
Example 4: hwo to make ana array of prime numbers in javascript
function sort_prime(num) {
var prime_num1 = [],
prime_num2 = [];
for (var i = 0; i <= num; i++) {
prime_num2.push(true);
}
for (var i = 2; i <= num; i++) {
if (prime_num2[i]) {
prime_num1.push(i);
for (var j = 1; i * j <= num; j++) {
prime_num2[i * j] = false;
}
}
}
return prime_num1;
}
console.log(sort_prime(5))