DeveloperBreeze

Prime Number Check and Prime Number Generation in JavaScript

// 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);
    }
}

Related Posts

More content you might like

Code
python

Binary Search in Python

No preview available for this content.

Jan 26, 2024
Read More
Code
python

Generate Fibonacci Sequence

def fibonacci(n):
    if n <= 1:
        return n
    else:
        return fibonacci(n-1) + fibonacci(n-2)

result = [fibonacci(i) for i in range(10)]
print('Fibonacci Sequence:', result)

Jan 26, 2024
Read More
Code
java

Swap Two Numbers Without Using a Temporary Variable

No preview available for this content.

Jan 26, 2024
Read More
Code
java

Calculate Factorial

No preview available for this content.

Jan 26, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Be the first to share your thoughts!