Leetcode Note: Go - Prime Arrangements

Prime Arrangements - LeetCode
https://leetcode.com/problems/prime-arrangements/

  • Go 言語で取り組んだメモ

回答

Go 0ms solution - LeetCode Discuss
https://leetcode.com/problems/prime-arrangements/discuss/495783/Go-0ms-solution

func numPrimeArrangements(n int) int {
    prime, noPrime := 0, 0
    for i := 1; i <= n; i++ {
        if isPrime(i) {
            prime++ 
        } else {
            noPrime++
        }
    }
    answer := 1
    for i := 2; i <= prime; i++ {
        answer *= i
        answer %= 1000000007
    }
    for i := 2; i <= noPrime; i++ {
        answer *= i
        answer %= 1000000007
    }
    return answer
}

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