- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the primorial of numbers - JavaScript
The primorial of a number n is equal to the product of first n prime numbers.
For example, if n = 4
Then, the output primorial(n) is,
2*3*5*7 = 210
We are required to write a JavaScript function that takes in a number and returns its primordial.
Example
Following is the code −
const num = 4; 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 0; } let count = 1, flag = 3; let prod = 2; while(count < num){ if(isPrime(flag)){ prod *= flag; count++; }; flag++; }; return prod; }; console.log(primorial(num));
Output
Following is the output in the console −
210
- Related Articles
- Primorial of a number in C++
- Find the greatest product of three numbers in JavaScript
- Program to find largest of three numbers - JavaScript
- Find the longest sub array of consecutive numbers with a while loop in JavaScript
- Check if a number is Primorial Prime or not in Python
- Check if a number is Primorial Prime or not in C++
- Find the largest palindrome number made from the product of two n digit numbers in JavaScript
- The sum of two consecutive numbers are 45 find the numbers.
- How to find and return the longest repeating series of numbers in array with JavaScript
- Counting the clusters of positive numbers - JavaScript Arrays
- Calculating the LCM of multiple numbers in JavaScript
- Finding the nth digit of natural numbers JavaScript
- JavaScript group array - find the sets of numbers that can be traveled to using the edges defined
- Taking the absolute sum of Array of Numbers in JavaScript
- The sum of the squares of two consecutive even numbers is 340. Find the numbers.

Advertisements