2020-06-13 23:45:37 +08:00
|
|
|
/**
|
2020-06-20 00:04:56 +08:00
|
|
|
* @file
|
2020-06-13 23:45:37 +08:00
|
|
|
* @brief
|
2023-06-17 06:08:45 +08:00
|
|
|
* A simple program to check if the given number is
|
|
|
|
* [Prime](https://en.wikipedia.org/wiki/Primality_test) or not.
|
2023-06-17 05:27:53 +08:00
|
|
|
* @details
|
2023-06-17 06:08:45 +08:00
|
|
|
* A prime number is any number that can be divided only by itself and 1. It
|
|
|
|
* must be positive and a whole number, therefore any prime number is part of
|
|
|
|
* the set of natural numbers. The majority of prime numbers are even numbers,
|
|
|
|
* with the exception of 2. This algorithm finds prime numbers using this
|
|
|
|
* information. additional ways to solve the prime check problem:
|
2023-06-17 05:27:53 +08:00
|
|
|
* https://cp-algorithms.com/algebra/primality_tests.html#practice-problems
|
|
|
|
* @author [Omkar Langhe](https://github.com/omkarlanghe)
|
|
|
|
* @author [ewd00010](https://github.com/ewd00010)
|
2020-06-13 23:45:37 +08:00
|
|
|
*/
|
2021-10-22 19:37:33 +08:00
|
|
|
|
2021-10-24 08:48:26 +08:00
|
|
|
#include <cassert> /// for assert
|
|
|
|
#include <iostream> /// for IO operations
|
2021-10-22 19:37:33 +08:00
|
|
|
|
2020-06-20 00:04:56 +08:00
|
|
|
/**
|
2023-06-17 05:27:53 +08:00
|
|
|
* @brief Mathematical algorithms
|
|
|
|
* @namespace
|
2020-06-13 23:45:37 +08:00
|
|
|
*/
|
2023-06-17 05:27:53 +08:00
|
|
|
namespace math {
|
2023-06-17 06:08:45 +08:00
|
|
|
/**
|
|
|
|
* @brief Function to check if the given number is prime or not.
|
|
|
|
* @param num number to be checked.
|
|
|
|
* @return true if number is a prime
|
|
|
|
* @return false if number is not a prime.
|
|
|
|
*/
|
|
|
|
bool is_prime(int64_t num) {
|
|
|
|
/*!
|
|
|
|
* Reduce all possibilities of a number which cannot be prime with the first
|
|
|
|
* 3 if, else if conditionals. Example: Since no even number, except 2 can
|
|
|
|
* be a prime number and the next prime we find after our checks is 5,
|
|
|
|
* we will start the for loop with i = 5. then for each loop we increment
|
|
|
|
* i by +6 and check if i or i+2 is a factor of the number; if it's a factor
|
|
|
|
* then we will return false. otherwise, true will be returned after the
|
|
|
|
* loop terminates at the terminating condition which is i*i <= num
|
2023-06-17 05:27:53 +08:00
|
|
|
*/
|
2023-06-17 06:08:45 +08:00
|
|
|
if (num <= 1) {
|
|
|
|
return false;
|
|
|
|
} else if (num == 2 || num == 3) {
|
|
|
|
return true;
|
|
|
|
} else if (num % 2 == 0 || num % 3 == 0) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
for (int64_t i = 5; i * i <= num; i = i + 6) {
|
|
|
|
if (num % i == 0 || num % (i + 2) == 0) {
|
|
|
|
return false;
|
2020-06-13 23:45:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-17 06:08:45 +08:00
|
|
|
return true;
|
|
|
|
}
|
2023-06-17 05:27:53 +08:00
|
|
|
} // namespace math
|
2020-06-13 23:45:37 +08:00
|
|
|
|
|
|
|
/**
|
2023-06-17 05:27:53 +08:00
|
|
|
* @brief Self-test implementations
|
|
|
|
* @returns void
|
2020-06-13 23:45:37 +08:00
|
|
|
*/
|
2023-06-17 05:27:53 +08:00
|
|
|
static void tests() {
|
|
|
|
assert(math::is_prime(1) == false);
|
|
|
|
assert(math::is_prime(2) == true);
|
|
|
|
assert(math::is_prime(3) == true);
|
|
|
|
assert(math::is_prime(4) == false);
|
|
|
|
assert(math::is_prime(-4) == false);
|
|
|
|
assert(math::is_prime(7) == true);
|
|
|
|
assert(math::is_prime(-7) == false);
|
|
|
|
assert(math::is_prime(19) == true);
|
|
|
|
assert(math::is_prime(50) == false);
|
|
|
|
assert(math::is_prime(115249) == true);
|
2020-06-20 00:04:56 +08:00
|
|
|
|
2023-06-17 05:27:53 +08:00
|
|
|
std::cout << "All tests have successfully passed!" << std::endl;
|
|
|
|
}
|
2020-06-20 00:04:56 +08:00
|
|
|
|
2023-06-17 05:27:53 +08:00
|
|
|
/**
|
|
|
|
* @brief Main function
|
|
|
|
* @returns 0 on exit
|
|
|
|
*/
|
|
|
|
int main() {
|
|
|
|
tests(); // perform self-tests implementations
|
2020-06-20 00:04:56 +08:00
|
|
|
return 0;
|
2020-06-13 23:45:37 +08:00
|
|
|
}
|