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.
Practical Learning Notes for Prime Number Check
This JavaScript program is part of the "Loop Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.