forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrime Arrangements.js
48 lines (33 loc) · 897 Bytes
/
Prime Arrangements.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
// Runtime: 80 ms (Top 70.37%) | Memory: 46.7 MB (Top 14.81%)
/**
* @param {number} n
* @return {number}
*/
var findNoOfPrimes = function(n){
let isPrime = new Array(n+1).fill(true);
let count =0;
for(let i=2; i<n;i++){
for(let j=i;j<=n;j++){
isPrime[j*i]= false;
}
}
isPrime.forEach(prime=> {
if(prime){
count++
}
})
return count-2;
}
var factorial = function(num){
let modulo = Math.pow(10,9) +7;
if(num<=0)
return 1;
return (BigInt(num) * BigInt(factorial(num-1)))%BigInt(modulo) ;
}
var numPrimeArrangements = function(n) {
let modulo = BigInt(Math.pow(10,9) +7);
let count = findNoOfPrimes(n);
let factorialPrime = factorial(count);
let factorialComposite = factorial(n-count);
return (BigInt(factorialPrime)*BigInt(factorialComposite))% (modulo);
};