var numPrimeArrangements = function(n) { let count = 1, pcount = 1; const dp = newArray(n+1).fill(0) dp[1] = 1; dp[2] = 1; const mod = 1e9 + 7; for (let i = 3; i <= n; i++) { if (isPrime(i)) { pcount++; dp[i] = (dp[i-1] * pcount) % mod; } else { count++; dp[i] = (dp[i-1] * count) % mod; } } return dp[n]; };
const isPrime = (n) => { if (n === 1) returnfalse; let max = Math.floor(Math.sqrt(n)); for (let i = max; i >= 2; i--) { if (n % i === 0) { returnfalse; } } returntrue; }