Published on January 26, 2024By DeveloperBreeze

// Function to check if a number is prime
function isPrime(num) {
    if (num <= 1) return false;
    if (num <= 3) return true;
    if (num % 2 === 0 || num % 3 === 0) return false;

    for (let i = 5; i * i <= num; i += 6) {
        if (num % i === 0 || num % (i + 2) === 0) return false;
    }

    return true;
}

// Usage: Generate prime numbers between 2 and 100
let primes = [];
for (let i = 2; i <= 100; i++) {
    if (isPrime(i)) {
        primes.push(i);
    }
}

Comments

Please log in to leave a comment.

Continue Reading:

Calculate Factorial

Published on January 26, 2024

pythonjava

Generate Fibonacci Sequence

Published on January 26, 2024

python

Binary Search in Python

Published on January 26, 2024

python