basics · intermediate · ~15 min

Is prime?

Bounded loops and the √n trick.

Challenge

Implement int is_prime(unsigned long n) returning 1 if n is prime, 0 otherwise. By convention, 0 and 1 are not prime.

Starter code

int is_prime(unsigned long n) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.