Prime Number Check
Program to check if a number is prime
JavaScript Prime Number Check Program
This program helps you to learn the fundamental structure and syntax of JavaScript programming.
// Prime number: divisible only by 1 and itself
// Examples: 2, 3, 5, 7, 11, 13, 17, 19
// Method 1: Basic check
function isPrime(n) {
if (n < 2) return false;
if (n === 2) return true;
if (n % 2 === 0) return false;
for (let i = 3; i < n; i += 2) {
if (n % i === 0) return false;
}
return true;
}
console.log("2:", isPrime(2));
console.log("4:", isPrime(4));
console.log("17:", isPrime(17));
console.log("20:", isPrime(20));
// Method 2: Optimized (check up to sqrt(n))
function isPrimeOptimized(n) {
if (n < 2) return false;
if (n === 2) return true;
if (n % 2 === 0) return false;
let sqrt = Math.sqrt(n);
for (let i = 3; i <= sqrt; i += 2) {
if (n % i === 0) return false;
}
return true;
}
console.log("\nOptimized:");
console.log("29:", isPrimeOptimized(29));
console.log("100:", isPrimeOptimized(100));
// Method 3: Find primes in range
function findPrimesInRange(start, end) {
let primes = [];
for (let i = start; i <= end; i++) {
if (isPrimeOptimized(i)) {
primes.push(i);
}
}
return primes;
}
console.log("\nPrimes between 10 and 30:");
console.log(findPrimesInRange(10, 30));
// Method 4: Count prime factors
function countPrimeFactors(n) {
let count = 0;
let factors = [];
for (let i = 2; i <= n; i++) {
while (n % i === 0) {
count++;
factors.push(i);
n /= i;
}
}
return { count, factors };
}
console.log("\nPrime factors of 60:");
console.log(countPrimeFactors(60));2: true
4: false
17: true
20: false
Optimized:
29: true
100: false
Primes between 10 and 30:
[ 11, 13, 17, 19, 23, 29 ]
Prime factors of 60:
{ count: 4, factors: [ 2, 2, 3, 5 ] }Understanding Prime Number Check
This program demonstrates prime number checking and related operations.
Prime Number Definition
A prime number is:
Special Cases:
Method 1: Basic Check
Check divisibility up to n-1:
for (let i = 3; i < n; i += 2) {
if (n % i === 0) return false;
}
Optimizations:
Method 2: Optimized (Square Root)
Only check up to √n:
let sqrt = Math.sqrt(n);
for (let i = 3; i <= sqrt; i += 2) {
if (n % i === 0) return false;
}
Why √n?
If n has a factor > √n, it must have a corresponding factor < √n.
Time Complexity:
Method 3: Find Primes in Range
Use prime check function:
for (let i = start; i <= end; i++) {
if (isPrimeOptimized(i)) {
primes.push(i);
}
}
Method 4: Prime Factorization
Find all prime factors:
for (let i = 2; i <= n; i++) {
while (n % i === 0) {
factors.push(i);
n /= i;
}
}
Example: 60
When to Use:
-
Basic
: Learning, small numbers
-
Optimized
: Production code, efficiency
-
Range
: Finding multiple primes
Let us now understand every line and the components of the above program.
Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.