[Golang] Primality Test - Optimized School Method
Go implementation of primality test - optimized school method. [1]
package main
import (
"fmt"
)
func IsPrime(n int) bool {
// Corner cases
if n <= 1 {
return false
}
if n <= 3 {
return true
}
// This is checked so that we can skip
// middle five numbers in below loop
if n%2 == 0 || n%3 == 0 {
return false
}
for i := 5; i*i <= n; i = i + 6 {
if n%i == 0 || n%(i+2) == 0 {
return false
}
}
return true
}
Tested on:
References:
[1] | Primality Test | Set 1 (Introduction and School Method) - GeeksforGeeks |
[2] |
[3] |
[4] |
[5] | Rosetta Code |
[6] |