Spirituality what is the javascript program for product of first 10 prime numbers

what is the javascript program for product of first 10 prime numbers

javascript
Copy code
// Define a function to check if a number is prime
function isPrime(n) {
if (n < 2) {
return false;
}
for (let i = 2; i <= Math.sqrt(n); i++) {
if (n % i === 0) {
return false;
}
}
return true;
}

// Initialize variables
let product = 1;
let count = 0;
let num = 2;

// Loop through the first 10 prime numbers
while (count < 10) {
if (isPrime(num)) {
product *= num;
count++;
}
num++;
}

// Print the product of the first 10 prime numbers
console.log(product);
This program defines a function isPrime(n) to check if a number is prime using a simple trial division method. It then initializes variables product, count, and num. The program then loops through the first 10 prime numbers, multiplying each prime number by the previous ones to calculate the product. Finally, it prints the product of the first 10 prime numbers, which is 2 * 3 * 5 * 7 * 11 * 13 * 17 * 19 * 23 * 29 = 6469693230.


There is no ads to display, Please add some

Related Post