Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find the primorial of numbers - JavaScript
The primorial of a number n is equal to the product of the first n prime numbers. This is different from factorial, which multiplies consecutive integers.
For example, if n = 4, we need the first 4 prime numbers: 2, 3, 5, and 7.
2 × 3 × 5 × 7 = 210
Let's write a JavaScript function that calculates the primorial of a given number.
Understanding Prime Numbers
First, we need a helper function to check if a number is prime:
const isPrime = n => {
if (n === 1) {
return false;
} else if (n === 2) {
return true;
} else {
for (let x = 2; x < n; x++) {
if (n % x === 0) {
return false;
}
}
return true;
}
};
// Test the isPrime function
console.log("First few prime numbers:");
for (let i = 1; i <= 10; i++) {
if (isPrime(i)) {
console.log(i + " is prime");
}
}
First few prime numbers: 2 is prime 3 is prime 5 is prime 7 is prime
Complete Primorial Function
Now we can implement the primorial function that finds the first n prime numbers and calculates their product:
const isPrime = n => {
if (n === 1) {
return false;
} else if (n === 2) {
return true;
} else {
for (let x = 2; x < n; x++) {
if (n % x === 0) {
return false;
}
}
return true;
}
};
const primorial = num => {
if (num === 0) {
return 1; // By definition, primorial(0) = 1
}
let count = 1; // We start with first prime (2)
let candidate = 3; // Next number to check
let product = 2; // Start with first prime
while (count < num) {
if (isPrime(candidate)) {
product *= candidate;
count++;
}
candidate++;
}
return product;
};
// Test with different values
console.log("Primorial of 1:", primorial(1));
console.log("Primorial of 4:", primorial(4));
console.log("Primorial of 5:", primorial(5));
console.log("Primorial of 6:", primorial(6));
Primorial of 1: 2 Primorial of 4: 210 Primorial of 5: 2310 Primorial of 6: 30030
How It Works
The algorithm works in these steps:
- Handle special case: if n = 0, return 1
- Start with the first prime number (2) and set product = 2
- Use a counter to track how many primes we've found
- Check each subsequent number for primality
- When we find a prime, multiply it to our product and increment counter
- Continue until we've found n prime numbers
Alternative Implementation with Array
Here's another approach that first generates an array of prime numbers:
const generatePrimes = count => {
const primes = [];
let candidate = 2;
while (primes.length < count) {
let isPrime = true;
for (let i = 2; i * i <= candidate; i++) {
if (candidate % i === 0) {
isPrime = false;
break;
}
}
if (isPrime) {
primes.push(candidate);
}
candidate++;
}
return primes;
};
const primorialArray = num => {
if (num === 0) return 1;
const primes = generatePrimes(num);
console.log(`First ${num} primes:`, primes);
return primes.reduce((product, prime) => product * prime, 1);
};
console.log("Primorial of 4:", primorialArray(4));
First 4 primes: [ 2, 3, 5, 7 ] Primorial of 4: 210
Comparison of Approaches
| Approach | Memory Usage | Readability | Performance |
|---|---|---|---|
| Direct calculation | Low | Good | Better |
| Array-based | Higher | Excellent | Good |
Conclusion
Primorial calculates the product of the first n prime numbers. The direct calculation approach is memory-efficient, while the array-based method offers better readability and debugging capabilities.
