|
1 | 1 | /**
|
2 | 2 | * @file
|
3 | 3 | * @brief
|
4 |
| - * A simple program to check if the given number is [Prime](https://en.wikipedia.org/wiki/Primality_test) or not. |
| 4 | + * A simple program to check if the given number is |
| 5 | + * [Prime](https://en.wikipedia.org/wiki/Primality_test) or not. |
5 | 6 | * @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: |
| 7 | + * A prime number is any number that can be divided only by itself and 1. It |
| 8 | + * must be positive and a whole number, therefore any prime number is part of |
| 9 | + * the set of natural numbers. The majority of prime numbers are even numbers, |
| 10 | + * with the exception of 2. This algorithm finds prime numbers using this |
| 11 | + * information. additional ways to solve the prime check problem: |
11 | 12 | * https://cp-algorithms.com/algebra/primality_tests.html#practice-problems
|
12 | 13 | * @author [Omkar Langhe](https://github.com/omkarlanghe)
|
13 | 14 | * @author [ewd00010](https://github.com/ewd00010)
|
|
21 | 22 | * @namespace
|
22 | 23 | */
|
23 | 24 | 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. |
| 25 | +/** |
| 26 | + * @brief Function to check if the given number is prime or not. |
| 27 | + * @param num number to be checked. |
| 28 | + * @return true if number is a prime |
| 29 | + * @return false if number is not a prime. |
| 30 | + */ |
| 31 | +bool is_prime(int64_t num) { |
| 32 | + /*! |
| 33 | + * Reduce all possibilities of a number which cannot be prime with the first |
| 34 | + * 3 if, else if conditionals. Example: Since no even number, except 2 can |
| 35 | + * be a prime number and the next prime we find after our checks is 5, |
| 36 | + * we will start the for loop with i = 5. then for each loop we increment |
| 37 | + * i by +6 and check if i or i+2 is a factor of the number; if it's a factor |
| 38 | + * then we will return false. otherwise, true will be returned after the |
| 39 | + * loop terminates at the terminating condition which is i*i <= num |
29 | 40 | */
|
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 |
| - } |
| 41 | + if (num <= 1) { |
| 42 | + return false; |
| 43 | + } else if (num == 2 || num == 3) { |
| 44 | + return true; |
| 45 | + } else if (num % 2 == 0 || num % 3 == 0) { |
| 46 | + return false; |
| 47 | + } else { |
| 48 | + for (int64_t i = 5; i * i <= num; i = i + 6) { |
| 49 | + if (num % i == 0 || num % (i + 2) == 0) { |
| 50 | + return false; |
51 | 51 | }
|
52 | 52 | }
|
53 |
| - return true; |
54 | 53 | }
|
| 54 | + return true; |
| 55 | +} |
55 | 56 | } // namespace math
|
56 | 57 |
|
57 | 58 | /**
|
|
0 commit comments