We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 7c7e1d2 + 1734cd0 commit 2537a71Copy full SHA for 2537a71
snippets/cpp/math-and-numbers/check-prime.md
@@ -0,0 +1,26 @@
1
+---
2
+title: Check Prime Number
3
+description: Check if an integer is a prime number
4
+tags: cpp, number, prime
5
+author: MihneaMoso
6
7
+
8
+```cpp
9
+bool is_prime(int n) {
10
+ if (n < 2) return false;
11
+ if (n == 2 || n == 3) return true;
12
+ if (n % 2 == 0) return false;
13
+ for (int i = 3; i * i <= n; i += 2) {
14
+ if (n % i == 0) return false;
15
+ }
16
+ return true;
17
+}
18
19
+// Usage
20
+#include <iostream>
21
22
+int main() {
23
+ std::cout << is_prime(29) << std::endl; // Output: 1
24
+ return 0;
25
26
+```
0 commit comments