[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 <68815218+realstealthninja@users.noreply.github.com>

* Update math/check_prime.cpp

Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com>

* 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 <68815218+realstealthninja@users.noreply.github.com>

* Update math/check_prime.cpp

Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com>

* Update math/check_prime.cpp

Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com>

* Update check_prime.cpp

removed cin/cout interaction

* Update check_prime.cpp

* Update math/check_prime.cpp

Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com>

* Update math/check_prime.cpp

Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com>

* Update math/check_prime.cpp

Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com>

* Update math/check_prime.cpp

Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com>

* Update math/check_prime.cpp

Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com>

* Update math/check_prime.cpp

Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com>

* Update math/check_prime.cpp

Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com>

* Update math/check_prime.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update check_prime.cpp

* Update math/check_prime.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

---------

Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com>
Co-authored-by: David Leal <halfpacho@gmail.com>
This commit is contained in:
ewd00010 2023-06-16 22:27:53 +01:00 committed by GitHub
parent 5704841875
commit 5ef7ad5cfe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,62 +1,83 @@
/**
* Copyright 2020 @author omkarlanghe
*
* @file
* A simple program to check if the given number if prime or not.
*
* @brief
* Reduced all possibilities of a number which cannot be prime.
* Eg: No even number, except 2 can be a prime number, hence we will increment
* our loop with i+6 jumping and check for i or i+2 to be a factor of the
* number; if it's a factor then we will return false otherwise true after the
* loop terminates at the terminating condition which is (i*i<=num)
* A simple program to check if the given number is [Prime](https://en.wikipedia.org/wiki/Primality_test) or not.
* @details
* 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:
* https://cp-algorithms.com/algebra/primality_tests.html#practice-problems
* @author [Omkar Langhe](https://github.com/omkarlanghe)
* @author [ewd00010](https://github.com/ewd00010)
*/
#include <cassert> /// for assert
#include <iostream> /// for IO operations
/**
* Function to check if the given number is prime or not.
* @param num number to be checked.
* @return if number is prime, it returns @ true, else it returns @ false.
* @brief Mathematical algorithms
* @namespace
*/
namespace math {
/**
* @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
*/
template <typename T>
bool is_prime(T num) {
bool result = true;
if (num <= 1) {
return false;
} else if (num == 2 || num == 3) {
return true;
} else if ((num % 2) == 0 || num % 3 == 0) {
} else if (num % 2 == 0 || num % 3 == 0) {
return false;
} else {
for (T i = 5; (i * i) <= (num); i = (i + 6)) {
if ((num % i) == 0 || (num % (i + 2) == 0)) {
result = false;
break;
for (int64_t i = 5; i * i <= num; i = i + 6) {
if (num % i == 0 || num % (i + 2) == 0) {
return false;
}
}
}
return (result);
return true;
}
} // namespace math
/**
* @brief Self-test implementations
* @returns void
*/
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);
std::cout << "All tests have successfully passed!" << std::endl;
}
/**
* Main function
* @brief Main function
* @returns 0 on exit
*/
int main() {
// perform self-test
assert(is_prime(50) == false);
assert(is_prime(115249) == true);
int num = 0;
std::cout << "Enter the number to check if it is prime or not" << std::endl;
std::cin >> num;
bool result = is_prime(num);
if (result) {
std::cout << num << " is a prime number" << std::endl;
} else {
std::cout << num << " is not a prime number" << std::endl;
}
tests(); // perform self-tests implementations
return 0;
}