From 1b5dee74c049857179b6da63c3696123b6684f28 Mon Sep 17 00:00:00 2001 From: Taj Date: Fri, 12 Jun 2020 23:47:32 +0100 Subject: [PATCH 1/4] feat: Added a function for finding the least common multiple (#840) * feat: Added a function for finding the least common multiple * feat: Miller-Rabin Primality Test (probabilistic) * Added test assertions * Mistakenly worked on a different branch * Doxygen comments * Comments changed --- math/least_common_multiple.cpp | 70 ++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 math/least_common_multiple.cpp diff --git a/math/least_common_multiple.cpp b/math/least_common_multiple.cpp new file mode 100644 index 000000000..741945716 --- /dev/null +++ b/math/least_common_multiple.cpp @@ -0,0 +1,70 @@ +/** + * Copyright 2020 @author tjgurwara99 + * @file + * + * A basic implementation of LCM function + */ + +#include +#include + +/** + * Function for finding greatest common divisor of two numbers. + * @params two integers x and y whose gcd we want to find. + * @return greatest common divisor of x and y. + */ +unsigned int gcd(unsigned int x, unsigned int y) { + if (x == 0) { + return y; + } + if (y == 0) { + return x; + } + if (x == y) { + return x; + } + if (x > y) { + // The following is valid because we have checked whether y == 0 + + int temp = x / y; + return gcd(y, x - temp * y); + } + // Again the following is valid because we have checked whether x == 0 + + int temp = y / x; + return gcd(x, y - temp * x); +} + +/** + * Function for finding the least common multiple of two numbers. + * @params integer x and y whose lcm we want to find. + * @return lcm of x and y using the relation x * y = gcd(x, y) * lcm(x, y) + */ +unsigned int lcm(unsigned int x, unsigned int y) { return x * y / gcd(x, y); } + +/** + * Function for testing the lcm() functions with some assert statements. + */ +void tests() { + // First test on lcm(5,10) == 10 + assert(((void)"LCM of 5 and 10 is 10 but lcm function gives a different " + "result.\n", + lcm(5, 10) == 10)); + std::cout << "First assertion passes: LCM of 5 and 10 is " << lcm(5, 10) + << std::endl; + + // Second test on lcm(2,3) == 6 as 2 and 3 are coprime (prime in fact) + assert(((void)"LCM of 2 and 3 is 6 but lcm function gives a different " + "result.\n", + lcm(2, 3) == 6)); + std::cout << "Second assertion passes: LCM of 2 and 3 is " << lcm(2, 3) + << std::endl; +} + +/** + * Main function + */ +int main() { + tests(); + return 0; +} From 282973453271a1df7d953e270b4ed96205457521 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Fri, 12 Jun 2020 22:47:55 +0000 Subject: [PATCH 2/4] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 5eb085bc0..f8ad585d5 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -115,6 +115,7 @@ * [Fibonacci](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/fibonacci.cpp) * [Greatest Common Divisor](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/greatest_common_divisor.cpp) * [Greatest Common Divisor Euclidean](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/greatest_common_divisor_euclidean.cpp) + * [Least Common Multiple](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/least_common_multiple.cpp) * [Modular Inverse Fermat Little Theorem](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/modular_inverse_fermat_little_theorem.cpp) * [Number Of Positive Divisors](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/number_of_positive_divisors.cpp) * [Power For Huge Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/power_for_huge_numbers.cpp) From 026557115ca9063b547996846f0d6e813fd90602 Mon Sep 17 00:00:00 2001 From: Omkar Langhe Date: Sat, 13 Jun 2020 21:15:37 +0530 Subject: [PATCH 3/4] Adding algorithm to check if number is prime or not. (#834) * Optimized algorithm to check if number is prime or not. * logic to check if given number is prime or not. * logic to check if given number is prime or not. * logic to check if given number is prime or not. * logic to check if given number is prime or not. * Included appropriate comments as per standards. * variable name renamed to num * added @file and @brief in comment. Also added template and variable name changed from is_prime to result * added @file and @brief in comment. Also added template and variable name changed from is_prime to result * added template parameter T type in loop --- math/check_prime.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 math/check_prime.cpp diff --git a/math/check_prime.cpp b/math/check_prime.cpp new file mode 100644 index 000000000..e517d774e --- /dev/null +++ b/math/check_prime.cpp @@ -0,0 +1,58 @@ +/** + * 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+2 jumping on all odd numbers only. + * If number is <= 1 or if it is even except 2, break the loop and return false telling number is not prime. + */ +#include +#include + /** + * 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. + */ +template +bool is_prime(T num) { + bool result = true; + if (num <= 1) { + return 0; + } else if (num == 2) { + return 1; + } else if ((num & 1) == 0) { + return 0; + } + if (num >= 3) { + for (T i = 3 ; (i*i) < (num) ; i = (i + 2)) { + if ((num % i) == 0) { + result = false; + break; + } + } + } + return (result); +} + +/** + * Main function + */ +int main() { + int num; + 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; + } + assert(is_prime(50) == false); + assert(is_prime(115249) == true); +} From 70a2aeedc369fbbc7313b768e0f5e9c673bcd86f Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 13 Jun 2020 15:45:53 +0000 Subject: [PATCH 4/4] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index f8ad585d5..1575561b8 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -107,6 +107,7 @@ ## Math * [Binary Exponent](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/binary_exponent.cpp) + * [Check Prime](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/check_prime.cpp) * [Double Factorial](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/double_factorial.cpp) * [Eulers Totient Function](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/eulers_totient_function.cpp) * [Extended Euclid Algorithm](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/extended_euclid_algorithm.cpp)