Check if a number is prime using a loop:
func isPrime(n int) bool {
if n < 2 {
return false
}
for i := 2; i*i <= n; i++ {
if n%i == 0 {
return false
}
}
return true
}
We only check divisors up to the square root. If no divisor is found, the number is prime. The early return makes this efficient.