mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Merge commit upstream/master into merge_upstream
Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com>
This commit is contained in:
commit
f55ab50cf2
@ -100,6 +100,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)
|
||||
|
58
math/check_prime.cpp
Normal file
58
math/check_prime.cpp
Normal file
@ -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 <iostream>
|
||||
#include <cassert>
|
||||
/**
|
||||
* 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<typename T>
|
||||
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);
|
||||
}
|
70
math/least_common_multiple.cpp
Normal file
70
math/least_common_multiple.cpp
Normal file
@ -0,0 +1,70 @@
|
||||
/**
|
||||
* Copyright 2020 @author tjgurwara99
|
||||
* @file
|
||||
*
|
||||
* A basic implementation of LCM function
|
||||
*/
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user