avinashrepo

avinash-repo

Posted on February 6, 2024

Prime Number

prime number wo number hoga jo use sqroot ke ander ke number se divide na ho
0,1 is not prime number

javascript
const number = 16;
const squareRoot = Math.sqrt(number);
console.log(squareRoot); // Output: 4

let otherNum=170
for (let i = 2; i <= Math.sqrt(otherNum); i++) {
    if (otherNum % i === 0) {
       console.log(i,otherNum,"notprime",Math.sqrt(otherNum))
        return false;
       // return false; // Not prime if divisible by any number other than 1 and itself
    }else{
      console.log(i,otherNum,"prime",Math.sqrt(otherNum))
      return true;
    }
}
Enter fullscreen mode Exit fullscreen mode

``To find prime numbers efficiently in JavaScript ES6, you can employ various algorithms. One commonly used method is the Sieve of Eratosthenes. Here's a step-by-step guide on how to implement it:

  1. Initialize an Array: Create an array of boolean values, where each index represents a number. Initialize all values to true, assuming initially all numbers are prime except 0 and 1.

  2. Iterate Through the Array: Starting from 2, iterate up to the square root of the highest number you want to check for primality. This is because any non-prime number must have a factor that is less than or equal to its square root.

  3. Mark Multiples as Non-Prime: For each prime number found, mark its multiples as non-prime in the array. Skip numbers that are already marked as non-prime.

  4. Identify Prime Numbers: After iterating through the array, the indices that are still marked as true represent prime numbers.

Here's a JavaScript ES6 code snippet implementing the Sieve of Eratosthenes algorithm:

`javascript
function findPrimes(limit) {
const primes = [];
const isPrime = new Array(limit + 1).fill(true);
isPrime[0] = isPrime[1] = false;

for (let i = 2; i <= Math.sqrt(limit); i++) {
    if (isPrime[i]) {
        for (let j = i * i; j <= limit; j += i) {
            isPrime[j] = false;
        }
    }
}

for (let i = 2; i <= limit; i++) {
    if (isPrime[i]) {
        primes.push(i);
    }
}

return primes;
Enter fullscreen mode Exit fullscreen mode

}

const limit = 100; // Adjust this to find primes up to a different limit
const primeNumbers = findPrimes(limit);
console.log(primeNumbers);
`

This code will efficiently find all prime numbers up to the specified limit and store them in an array. Adjust the limit variable to find prime numbers within a different range. This approach is optimized for performance and commonly used in interviews and practical implementations.

Certainly! Another approach to find prime numbers is the trial division method. Here's how it works:

  1. Iterate Through Numbers: Start iterating from 2 up to the number you want to check for primality.

  2. Check Divisibility: For each number, check if it is divisible by any integer between 2 and the square root of the number. If it is divisible by any of these numbers, it's not a prime number.

  3. Identify Prime Numbers: If none of the numbers between 2 and the square root divide evenly into the number, then it's a prime number.

Here's a simplified JavaScript ES6 code implementing the trial division method:

`javascript
function isPrime(num) {
if (num <= 1) return false; // 0 and 1 are not prime

for (let i = 2; i <= Math.sqrt(num); i++) {
    if (num % i === 0) {
        return false; // Not prime if divisible by any number other than 1 and itself
    }
}

return true; // Prime if not divisible by any number other than 1 and itself
Enter fullscreen mode Exit fullscreen mode

}

// Example usage
console.log(isPrime(23)); // Output: true
console.log(isPrime(24)); // Output: false
`

In this code, the isPrime function takes a number as input and returns true if it's prime, false otherwise. It checks divisibility by iterating through numbers from 2 up to the square root of the given number. If any of these numbers divides evenly into the given number, it's not prime. Otherwise, it's prime.

💖 💪 🙅 🚩
avinashrepo
avinash-repo

Posted on February 6, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

How JavaScript works
javascript How JavaScript works

November 8, 2024

A Beginner's Guide to JavaScript Closures
javascript A Beginner's Guide to JavaScript Closures

November 5, 2024

Type Coercion in JavaScript Explained
javascript Type Coercion in JavaScript Explained

November 18, 2024

Introduction to Asynchronous JavaScript
javascript Introduction to Asynchronous JavaScript

October 14, 2024

Strings -- Manipulating the Immutable.
javascript Strings -- Manipulating the Immutable.

November 15, 2024