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

Continue Reading

Discover more amazing content handpicked just for you

Code
python

Binary Search in Python

No preview available for this content.

Jan 26, 2024
Read More
Code
python

Generate Fibonacci Sequence

No preview available for this content.

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

public static long factorial(int n) {
    if (n == 0 || n == 1) {
        return 1;
    }
    return n * factorial(n - 1);
}

int number = 5;
long result = factorial(number);
System.out.println("Factorial of " + number + " is " + result);

Jan 26, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!