luoheng
10/28/2019 - 1:10 PM

numPrimeArrangements


import "math"

func isPrime(n int) bool {
    if n < 2 {
        return false
    }
    if n == 2 {
        return true
    }
    for i := 2; i <= int(math.Sqrt(float64(n))); i++ {
        if n % i == 0 {
            return false
        }
    }
    return true
}

func numPrimeArrangements(n int) int {
    nPrimes := 0
    for i := 1; i <= n; i++ {
        if isPrime(i) {
            nPrimes++
        }
    }
    sum := 1
    for i := nPrimes; i >= 1; i-- {
        sum *= i
        sum %= 1000000007
    }
    for i := n - nPrimes; i >= 1; i-- {
        sum *= i
        sum %= 1000000007
    }
    return sum
}