Skip to content

Commit 5ef7ad5

Browse files
ewd00010realstealthninjaPanquesito7
authored
[feat/docs/fix]: fit check_prime.cpp to guidelines (#2460)
* check_prime_update docs: added links to file @brief and @details. moved previous @brief to be with function, made comments easier to read in general and added boilerplate documentation to functions. chore: removed bool result and most brackets in is_prime. Moved assert tests to their own function and added more in, test success now returns message. * Delete cmake-build-debug directory * clang-format * original box_stacking documentation start point * Update math/check_prime.cpp Co-authored-by: realstealthninja <[email protected]> * Update math/check_prime.cpp Co-authored-by: realstealthninja <[email protected]> * Delete box_stacking.cpp * Update check_prime.cpp improved files @details text, removed generic t template - replaced with long long * Update math/check_prime.cpp Co-authored-by: realstealthninja <[email protected]> * Update math/check_prime.cpp Co-authored-by: realstealthninja <[email protected]> * Update math/check_prime.cpp Co-authored-by: realstealthninja <[email protected]> * Update check_prime.cpp removed cin/cout interaction * Update check_prime.cpp * Update math/check_prime.cpp Co-authored-by: realstealthninja <[email protected]> * Update math/check_prime.cpp Co-authored-by: realstealthninja <[email protected]> * Update math/check_prime.cpp Co-authored-by: realstealthninja <[email protected]> * Update math/check_prime.cpp Co-authored-by: realstealthninja <[email protected]> * Update math/check_prime.cpp Co-authored-by: realstealthninja <[email protected]> * Update math/check_prime.cpp Co-authored-by: realstealthninja <[email protected]> * Update math/check_prime.cpp Co-authored-by: realstealthninja <[email protected]> * Update math/check_prime.cpp Co-authored-by: David Leal <[email protected]> * Update check_prime.cpp * Update math/check_prime.cpp Co-authored-by: David Leal <[email protected]> --------- Co-authored-by: realstealthninja <[email protected]> Co-authored-by: David Leal <[email protected]>
1 parent 5704841 commit 5ef7ad5

File tree

1 file changed

+63
-42
lines changed

1 file changed

+63
-42
lines changed

math/check_prime.cpp

Lines changed: 63 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,83 @@
11
/**
2-
* Copyright 2020 @author omkarlanghe
3-
*
42
* @file
5-
* A simple program to check if the given number if prime or not.
6-
*
73
* @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)
1314
*/
1415

1516
#include <cassert> /// for assert
1617
#include <iostream> /// for IO operations
1718

1819
/**
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
2222
*/
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+
}
3751
}
3852
}
53+
return true;
3954
}
40-
return (result);
41-
}
55+
} // namespace math
4256

4357
/**
44-
* Main function
58+
* @brief Self-test implementations
59+
* @returns void
4560
*/
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);
5072

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+
}
6075

76+
/**
77+
* @brief Main function
78+
* @returns 0 on exit
79+
*/
80+
int main() {
81+
tests(); // perform self-tests implementations
6182
return 0;
6283
}

0 commit comments

Comments
 (0)