Skip to content

Commit f55ab50

Browse files
committed
Merge commit upstream/master into merge_upstream
Signed-off-by: Krishna Vedala <[email protected]>
2 parents e2996ce + 70a2aee commit f55ab50

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100

101101
## Math
102102
* [Binary Exponent](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/binary_exponent.cpp)
103+
* [Check Prime](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/check_prime.cpp)
103104
* [Double Factorial](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/double_factorial.cpp)
104105
* [Eulers Totient Function](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/eulers_totient_function.cpp)
105106
* [Extended Euclid Algorithm](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/extended_euclid_algorithm.cpp)

math/check_prime.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Copyright 2020 @author omkarlanghe
3+
*
4+
* @file
5+
* A simple program to check if the given number if prime or not.
6+
*
7+
* @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 our loop with i+2 jumping on all odd numbers only.
10+
* If number is <= 1 or if it is even except 2, break the loop and return false telling number is not prime.
11+
*/
12+
#include <iostream>
13+
#include <cassert>
14+
/**
15+
* Function to check if the given number is prime or not.
16+
* @param num number to be checked.
17+
* @return if number is prime, it returns @ true, else it returns @ false.
18+
*/
19+
template<typename T>
20+
bool is_prime(T num) {
21+
bool result = true;
22+
if (num <= 1) {
23+
return 0;
24+
} else if (num == 2) {
25+
return 1;
26+
} else if ((num & 1) == 0) {
27+
return 0;
28+
}
29+
if (num >= 3) {
30+
for (T i = 3 ; (i*i) < (num) ; i = (i + 2)) {
31+
if ((num % i) == 0) {
32+
result = false;
33+
break;
34+
}
35+
}
36+
}
37+
return (result);
38+
}
39+
40+
/**
41+
* Main function
42+
*/
43+
int main() {
44+
int num;
45+
std::cout << "Enter the number to check if it is prime or not" <<
46+
std::endl;
47+
std::cin >> num;
48+
bool result = is_prime(num);
49+
if (result) {
50+
std::cout << num << " is a prime number" <<
51+
std::endl;
52+
} else {
53+
std::cout << num << " is not a prime number" <<
54+
std::endl;
55+
}
56+
assert(is_prime(50) == false);
57+
assert(is_prime(115249) == true);
58+
}

math/least_common_multiple.cpp

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Copyright 2020 @author tjgurwara99
3+
* @file
4+
*
5+
* A basic implementation of LCM function
6+
*/
7+
8+
#include <cassert>
9+
#include <iostream>
10+
11+
/**
12+
* Function for finding greatest common divisor of two numbers.
13+
* @params two integers x and y whose gcd we want to find.
14+
* @return greatest common divisor of x and y.
15+
*/
16+
unsigned int gcd(unsigned int x, unsigned int y) {
17+
if (x == 0) {
18+
return y;
19+
}
20+
if (y == 0) {
21+
return x;
22+
}
23+
if (x == y) {
24+
return x;
25+
}
26+
if (x > y) {
27+
// The following is valid because we have checked whether y == 0
28+
29+
int temp = x / y;
30+
return gcd(y, x - temp * y);
31+
}
32+
// Again the following is valid because we have checked whether x == 0
33+
34+
int temp = y / x;
35+
return gcd(x, y - temp * x);
36+
}
37+
38+
/**
39+
* Function for finding the least common multiple of two numbers.
40+
* @params integer x and y whose lcm we want to find.
41+
* @return lcm of x and y using the relation x * y = gcd(x, y) * lcm(x, y)
42+
*/
43+
unsigned int lcm(unsigned int x, unsigned int y) { return x * y / gcd(x, y); }
44+
45+
/**
46+
* Function for testing the lcm() functions with some assert statements.
47+
*/
48+
void tests() {
49+
// First test on lcm(5,10) == 10
50+
assert(((void)"LCM of 5 and 10 is 10 but lcm function gives a different "
51+
"result.\n",
52+
lcm(5, 10) == 10));
53+
std::cout << "First assertion passes: LCM of 5 and 10 is " << lcm(5, 10)
54+
<< std::endl;
55+
56+
// Second test on lcm(2,3) == 6 as 2 and 3 are coprime (prime in fact)
57+
assert(((void)"LCM of 2 and 3 is 6 but lcm function gives a different "
58+
"result.\n",
59+
lcm(2, 3) == 6));
60+
std::cout << "Second assertion passes: LCM of 2 and 3 is " << lcm(2, 3)
61+
<< std::endl;
62+
}
63+
64+
/**
65+
* Main function
66+
*/
67+
int main() {
68+
tests();
69+
return 0;
70+
}

0 commit comments

Comments
 (0)