TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
primality_test.cpp
Go to the documentation of this file.
1
12#include <iostream>
13
18bool IsPrime(int number) {
19 if (((!(number & 1)) && number != 2) || (number < 2) ||
20 (number % 3 == 0 && number != 3))
21 return false;
22
23 for (int k = 1; 36 * k * k - 12 * k < number; ++k) {
24 if ((number % (6 * k + 1) == 0) || (number % (6 * k - 1) == 0))
25 return false;
26 }
27 return true;
28}
29
31int main() {
32 // Main Function
33 std::cout << "Enter the value of n to check if Prime\n";
34 int n;
35 std::cin >> n;
36 if (IsPrime(n))
37 std::cout << n << " is Prime" << std::endl;
38 else
39 std::cout << n << " is not Prime" << std::endl;
40
41 return 0;
42}
bool IsPrime(int number)
int main()