|
1 | 1 | /**
|
2 |
| - * Copyright 2020 @author omkarlanghe |
3 |
| - * |
4 | 2 | * @file
|
5 |
| - * A simple program to check if the given number if prime or not. |
6 |
| - * |
7 | 3 | * @brief
|
8 |
| - * Reduced all possibilities of a number which cannot be prime. |
9 |
| - * Eg: No even number, except 2 can be a prime number, hence we will increment |
10 |
| - * our loop with i+6 jumping and check for i or i+2 to be a factor of the |
11 |
| - * number; if it's a factor then we will return false otherwise true after the |
12 |
| - * loop terminates at the terminating condition which is (i*i<=num) |
| 4 | + * A simple program to check if the given number is [Prime](https://en.wikipedia.org/wiki/Primality_test) or not. |
| 5 | + * @details |
| 6 | + * A prime number is any number that can be divided only by itself and 1. It must |
| 7 | + * be positive and a whole number, therefore any prime number is part of the |
| 8 | + * set of natural numbers. The majority of prime numbers are even numbers, with |
| 9 | + * the exception of 2. This algorithm finds prime numbers using this information. |
| 10 | + * additional ways to solve the prime check problem: |
| 11 | + * https://cp-algorithms.com/algebra/primality_tests.html#practice-problems |
| 12 | + * @author [Omkar Langhe](https://github.com/omkarlanghe) |
| 13 | + * @author [ewd00010](https://github.com/ewd00010) |
13 | 14 | */
|
14 | 15 |
|
15 | 16 | #include <cassert> /// for assert
|
16 | 17 | #include <iostream> /// for IO operations
|
17 | 18 |
|
18 | 19 | /**
|
19 |
| - * Function to check if the given number is prime or not. |
20 |
| - * @param num number to be checked. |
21 |
| - * @return if number is prime, it returns @ true, else it returns @ false. |
| 20 | + * @brief Mathematical algorithms |
| 21 | + * @namespace |
22 | 22 | */
|
23 |
| -template <typename T> |
24 |
| -bool is_prime(T num) { |
25 |
| - bool result = true; |
26 |
| - if (num <= 1) { |
27 |
| - return false; |
28 |
| - } else if (num == 2 || num == 3) { |
29 |
| - return true; |
30 |
| - } else if ((num % 2) == 0 || num % 3 == 0) { |
31 |
| - return false; |
32 |
| - } else { |
33 |
| - for (T i = 5; (i * i) <= (num); i = (i + 6)) { |
34 |
| - if ((num % i) == 0 || (num % (i + 2) == 0)) { |
35 |
| - result = false; |
36 |
| - break; |
| 23 | +namespace math { |
| 24 | + /** |
| 25 | + * @brief Function to check if the given number is prime or not. |
| 26 | + * @param num number to be checked. |
| 27 | + * @return true if number is a prime |
| 28 | + * @return false if number is not a prime. |
| 29 | + */ |
| 30 | + bool is_prime(int64_t num) { |
| 31 | + /*! |
| 32 | + * Reduce all possibilities of a number which cannot be prime with the first |
| 33 | + * 3 if, else if conditionals. Example: Since no even number, except 2 can |
| 34 | + * be a prime number and the next prime we find after our checks is 5, |
| 35 | + * we will start the for loop with i = 5. then for each loop we increment |
| 36 | + * i by +6 and check if i or i+2 is a factor of the number; if it's a factor |
| 37 | + * then we will return false. otherwise, true will be returned after the |
| 38 | + * loop terminates at the terminating condition which is i*i <= num |
| 39 | + */ |
| 40 | + if (num <= 1) { |
| 41 | + return false; |
| 42 | + } else if (num == 2 || num == 3) { |
| 43 | + return true; |
| 44 | + } else if (num % 2 == 0 || num % 3 == 0) { |
| 45 | + return false; |
| 46 | + } else { |
| 47 | + for (int64_t i = 5; i * i <= num; i = i + 6) { |
| 48 | + if (num % i == 0 || num % (i + 2) == 0) { |
| 49 | + return false; |
| 50 | + } |
37 | 51 | }
|
38 | 52 | }
|
| 53 | + return true; |
39 | 54 | }
|
40 |
| - return (result); |
41 |
| -} |
| 55 | +} // namespace math |
42 | 56 |
|
43 | 57 | /**
|
44 |
| - * Main function |
| 58 | + * @brief Self-test implementations |
| 59 | + * @returns void |
45 | 60 | */
|
46 |
| -int main() { |
47 |
| - // perform self-test |
48 |
| - assert(is_prime(50) == false); |
49 |
| - assert(is_prime(115249) == true); |
| 61 | +static void tests() { |
| 62 | + assert(math::is_prime(1) == false); |
| 63 | + assert(math::is_prime(2) == true); |
| 64 | + assert(math::is_prime(3) == true); |
| 65 | + assert(math::is_prime(4) == false); |
| 66 | + assert(math::is_prime(-4) == false); |
| 67 | + assert(math::is_prime(7) == true); |
| 68 | + assert(math::is_prime(-7) == false); |
| 69 | + assert(math::is_prime(19) == true); |
| 70 | + assert(math::is_prime(50) == false); |
| 71 | + assert(math::is_prime(115249) == true); |
50 | 72 |
|
51 |
| - int num = 0; |
52 |
| - std::cout << "Enter the number to check if it is prime or not" << std::endl; |
53 |
| - std::cin >> num; |
54 |
| - bool result = is_prime(num); |
55 |
| - if (result) { |
56 |
| - std::cout << num << " is a prime number" << std::endl; |
57 |
| - } else { |
58 |
| - std::cout << num << " is not a prime number" << std::endl; |
59 |
| - } |
| 73 | + std::cout << "All tests have successfully passed!" << std::endl; |
| 74 | +} |
60 | 75 |
|
| 76 | +/** |
| 77 | + * @brief Main function |
| 78 | + * @returns 0 on exit |
| 79 | + */ |
| 80 | +int main() { |
| 81 | + tests(); // perform self-tests implementations |
61 | 82 | return 0;
|
62 | 83 | }
|
0 commit comments