-
Notifications
You must be signed in to change notification settings - Fork 0
/
6kyu-primorial-of-a-number.js
57 lines (43 loc) · 1.74 KB
/
6kyu-primorial-of-a-number.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Definition (Primorial Of a Number)
// Is similar to factorial of a number, In primorial, not all the natural numbers get multiplied, only prime numbers are multiplied to calculate the primorial of a number. It's denoted with P#.
// Task
// Given a number N , calculate its primorial. !alt !alt
// Notes
// Only positive numbers will be passed (N > 0) .
// Input >> Output Examples:
// 1- numPrimorial (3) ==> return (30)
// Explanation:
// Since the passed number is (3) ,Then the primorial should obtained by multiplying 2 * 3 * 5 = 30 .
// Mathematically written as , P3# = 30 .
// 2- numPrimorial (5) ==> return (2310)
// Explanation:
// Since the passed number is (5) ,Then the primorial should obtained by multiplying 2 * 3 * 5 * 7 * 11 = 2310 .
// Mathematically written as , P5# = 2310 .
// 3- numPrimorial (6) ==> return (30030)
// Explanation:
// Since the passed number is (6) ,Then the primorial should obtained by multiplying 2 * 3 * 5 * 7 * 11 * 13 = 30030 .
// Mathematically written as , P6# = 30030 .
// Playing with Numbers Series
// Playing With Lists/Arrays Series
// For More Enjoyable Katas
// ALL translations are welcomed
// Enjoy Learning !!
// Zizou
function numPrimorial(n) {
const primeNums = [];
function isPrime(num) {
for (let i = 2, s = Math.sqrt(num); i <= s; i += 1) {
if (num % i === 0) return false;
}
return num > 1;
}
for (let i = 0; i < Infinity; i += 1) {
if (isPrime(i)) {
primeNums.push(i);
}
if (primeNums.length === n) {
break;
}
}
return primeNums.reduce((cum, val) => cum * val);
}