Primality test in Go
Trial division up to the square root, which is enough.
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
}
Keywords and builtins used here
boolforfuncifintreturn
The run, in numbers
- Lines
- 11
- Characters to type
- 125
- Tokens
- 44
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 29 seconds.
Step 5 of 6 in Functions, step 14 of 30 in Language basics.