Primality test in C
Trial division up to the square root, which is enough.
int is_prime(int n) {
if (n < 2)
return 0;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return 0;
}
return 1;
}
Keywords and builtins used here
forifintis_primereturn
The run, in numbers
- Lines
- 9
- Characters to type
- 117
- Tokens
- 52
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 35 seconds.
Step 4 of 7 in Functions, step 18 of 35 in Language basics.