-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
primality_test.cpp
42 lines (38 loc) · 1.1 KB
/
primality_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* @file
* @brief [Primality test](https://en.wikipedia.org/wiki/Primality_test)
* implementation.
*
* A simple and efficient implementation of a function to test if a number is
* prime, based on the fact that
* > Every Prime number, except 2 and 3, are of the form \f$6k\pm1\f$ for
* > integer values of k.
* This gives a 3x speed improvement.
*/
#include <iostream>
/** Check if a number is prime
* \param[in] number number to check
* \returns true if prime else false
*/
bool IsPrime(int number) {
if (((!(number & 1)) && number != 2) || (number < 2) ||
(number % 3 == 0 && number != 3))
return false;
for (int k = 1; 36 * k * k - 12 * k < number; ++k) {
if ((number % (6 * k + 1) == 0) || (number % (6 * k - 1) == 0))
return false;
}
return true;
}
/** main function */
int main() {
// Main Function
std::cout << "Enter the value of n to check if Prime\n";
int n;
std::cin >> n;
if (IsPrime(n))
std::cout << n << " is Prime" << std::endl;
else
std::cout << n << " is not Prime" << std::endl;
return 0;
}