Skip to content

Instantly share code, notes, and snippets.

@potat-dev
Created February 13, 2022 02:20
Show Gist options
  • Save potat-dev/0a04602fd9ba136d44179d531447224f to your computer and use it in GitHub Desktop.
Save potat-dev/0a04602fd9ba136d44179d531447224f to your computer and use it in GitHub Desktop.
Primality test in C++
int is_prime(int n) {
if (n < 3) return (n == 2);
if (~n & 1) return 0;
for (int i = 3; i*i <= n; i += 2)
if (n % i == 0) return 0;
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment