Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions bit_manipulation/count_bits_flip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ std::uint64_t countBitsFlip(
std::int64_t B) { // int64_t is preferred over int so that
// no Overflow can be there.

int count =
std::uint64_t count =
0; // "count" variable is used to count number of bits flip of the
// number A to form B in binary representation of number 'n'
// using uint64_t instead of int to prevent overflows and match
// the function return type
A = A ^ B;
while (A) {
A = A & (A - 1);
Expand Down Expand Up @@ -77,7 +79,7 @@ static void test() {
assert(bit_manipulation::count_bits_flip::countBitsFlip(21, 22) == 2);
// A = 7, B = 786 return 5
assert(bit_manipulation::count_bits_flip::countBitsFlip(7, 786) == 5);
std::cout << "All test cases successfully passed!" << std::endl;
std::cout << "All test cases successfully passed!\n";
}
/**
* @brief Main function
Expand Down
11 changes: 9 additions & 2 deletions math/factorial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,17 @@ uint64_t factorial(uint8_t n) {
if (n > 20) {
throw std::invalid_argument("maximum value is 20\n");
}
if (n == 0) {
// factorial of both 0 and 1 is 1
if (n == 0 || n == 1) {
return 1;
}
return n * factorial(n - 1);

uint64_t result{1};
for (; n > 1; --n) {
result *= n;
}

return result;
}
} // namespace math

Expand Down
16 changes: 4 additions & 12 deletions math/magic_number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,8 @@ namespace math {
* @param n number to be checked.
* @return if number is a magic number, returns true, else false.
*/
bool magic_number(const uint64_t &n) {
if (n <= 0) {
return false;
}
// result stores the modulus of @param n with 9
uint64_t result = n % 9;
// if result is 1 then the number is a magic number else not
if (result == 1) {
return true;
} else {
return false;
}
constexpr inline bool magic_number(const uint64_t &n) noexcept {
return n > 0 && (n % 9) == 1;
}
} // namespace math

Expand Down Expand Up @@ -69,6 +59,8 @@ static void tests() {
std::cout << "Test 5:\t n=-35\n";
assert(math::magic_number(-35) == false);
std::cout << "passed\n";

std::cout << "\nAll tests passed!\n";
}

/**
Expand Down
102 changes: 51 additions & 51 deletions math/volume.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace math {
* @returns The volume of the cube
*/
template <typename T>
T cube_volume(T length) {
constexpr inline T cube_volume(T length) noexcept {
return std::pow(length, 3);
}

Expand All @@ -38,7 +38,7 @@ T cube_volume(T length) {
* @returns The volume of the rectangular prism
*/
template <typename T>
T rect_prism_volume(T length, T width, T height) {
constexpr inline T rect_prism_volume(T length, T width, T height) noexcept {
return length * width * height;
}

Expand All @@ -50,7 +50,7 @@ T rect_prism_volume(T length, T width, T height) {
* @returns The volume of the cone
*/
template <typename T>
T cone_volume(T radius, T height, double PI = 3.14) {
constexpr inline T cone_volume(T radius, T height, double PI = 3.14) noexcept {
return std::pow(radius, 2) * PI * height / 3;
}

Expand All @@ -64,7 +64,7 @@ T cone_volume(T radius, T height, double PI = 3.14) {
* @returns The volume of the triangular prism
*/
template <typename T>
T triangle_prism_volume(T base, T height, T depth) {
constexpr inline T triangle_prism_volume(T base, T height, T depth) noexcept {
return base * height * depth / 2;
}

Expand All @@ -77,7 +77,7 @@ T triangle_prism_volume(T base, T height, T depth) {
* @returns The volume of the pyramid
*/
template <typename T>
T pyramid_volume(T length, T width, T height) {
constexpr inline T pyramid_volume(T length, T width, T height) noexcept {
return length * width * height / 3;
}

Expand All @@ -88,7 +88,7 @@ T pyramid_volume(T length, T width, T height) {
* @returns The volume of the sphere
*/
template <typename T>
T sphere_volume(T radius, double PI = 3.14) {
constexpr inline T sphere_volume(T radius, double PI = 3.14) noexcept {
return PI * std::pow(radius, 3) * 4 / 3;
}

Expand All @@ -100,7 +100,7 @@ T sphere_volume(T radius, double PI = 3.14) {
* @returns The volume of the cylinder
*/
template <typename T>
T cylinder_volume(T radius, T height, double PI = 3.14) {
constexpr inline T cylinder_volume(T radius, T height, double PI = 3.14) noexcept {
return PI * std::pow(radius, 2) * height;
}
} // namespace math
Expand All @@ -109,7 +109,7 @@ T cylinder_volume(T radius, T height, double PI = 3.14) {
* @brief Self-test implementations
* @returns void
*/
static void test() {
static void test() noexcept {
// Input variables
uint32_t int_length = 0; // 32 bit integer length input
uint32_t int_width = 0; // 32 bit integer width input
Expand All @@ -132,12 +132,12 @@ static void test() {
int_expected = 125;
int_volume = math::cube_volume(int_length);

std::cout << "VOLUME OF A CUBE" << std::endl;
std::cout << "Input Length: " << int_length << std::endl;
std::cout << "Expected Output: " << int_expected << std::endl;
std::cout << "Output: " << int_volume << std::endl;
std::cout << "VOLUME OF A CUBE\n";
std::cout << "Input Length: " << int_length;
std::cout << "\nExpected Output: " << int_expected;
std::cout << "\nOutput: " << int_volume;
assert(int_volume == int_expected);
std::cout << "TEST PASSED" << std::endl << std::endl;
std::cout << "\nTEST PASSED\n\n";

// 2nd test
int_length = 4;
Expand All @@ -146,28 +146,28 @@ static void test() {
int_expected = 60;
int_volume = math::rect_prism_volume(int_length, int_width, int_height);

std::cout << "VOLUME OF A RECTANGULAR PRISM" << std::endl;
std::cout << "Input Length: " << int_length << std::endl;
std::cout << "Input Width: " << int_width << std::endl;
std::cout << "Input Height: " << int_height << std::endl;
std::cout << "Expected Output: " << int_expected << std::endl;
std::cout << "Output: " << int_volume << std::endl;
std::cout << "VOLUME OF A RECTANGULAR PRISM\n";
std::cout << "Input Length: " << int_length;
std::cout << "\nInput Width: " << int_width;
std::cout << "\nInput Height: " << int_height;
std::cout << "\nExpected Output: " << int_expected;
std::cout << "\nOutput: " << int_volume;
assert(int_volume == int_expected);
std::cout << "TEST PASSED" << std::endl << std::endl;
std::cout << "\nTEST PASSED\n\n";

// 3rd test
double_radius = 5;
double_height = 7;
double_expected = 183.16666666666666; // truncated to 14 decimal places
double_volume = math::cone_volume(double_radius, double_height);

std::cout << "VOLUME OF A CONE" << std::endl;
std::cout << "Input Radius: " << double_radius << std::endl;
std::cout << "Input Height: " << double_height << std::endl;
std::cout << "Expected Output: " << double_expected << std::endl;
std::cout << "Output: " << double_volume << std::endl;
std::cout << "VOLUME OF A CONE\n";
std::cout << "Input Radius: " << double_radius;
std::cout << "\nInput Height: " << double_height;
std::cout << "\nExpected Output: " << double_expected;
std::cout << "\nOutput: " << double_volume;
assert(double_volume == double_expected);
std::cout << "TEST PASSED" << std::endl << std::endl;
std::cout << "\nTEST PASSED\n\n";

// 4th test
int_base = 3;
Expand All @@ -176,14 +176,14 @@ static void test() {
int_expected = 30;
int_volume = math::triangle_prism_volume(int_base, int_height, int_depth);

std::cout << "VOLUME OF A TRIANGULAR PRISM" << std::endl;
std::cout << "Input Base: " << int_base << std::endl;
std::cout << "Input Height: " << int_height << std::endl;
std::cout << "Input Depth: " << int_depth << std::endl;
std::cout << "Expected Output: " << int_expected << std::endl;
std::cout << "Output: " << int_volume << std::endl;
std::cout << "VOLUME OF A TRIANGULAR PRISM\n";
std::cout << "Input Base: " << int_base;
std::cout << "\nInput Height: " << int_height;
std::cout << "\nInput Depth: " << int_depth;
std::cout << "\nExpected Output: " << int_expected;
std::cout << "\nOutput: " << int_volume;
assert(int_volume == int_expected);
std::cout << "TEST PASSED" << std::endl << std::endl;
std::cout << "\nTEST PASSED\n\n";

// 5th test
int_length = 10;
Expand All @@ -192,40 +192,40 @@ static void test() {
int_expected = 50;
int_volume = math::pyramid_volume(int_length, int_width, int_height);

std::cout << "VOLUME OF A PYRAMID" << std::endl;
std::cout << "Input Length: " << int_length << std::endl;
std::cout << "Input Width: " << int_width << std::endl;
std::cout << "Input Height: " << int_height << std::endl;
std::cout << "Expected Output: " << int_expected << std::endl;
std::cout << "Output: " << int_volume << std::endl;
std::cout << "VOLUME OF A PYRAMID\n";
std::cout << "Input Length: " << int_length;
std::cout << "\nInput Width: " << int_width;
std::cout << "\nInput Height: " << int_height;
std::cout << "\nExpected Output: " << int_expected;
std::cout << "\nOutput: " << int_volume;
assert(int_volume == int_expected);
std::cout << "TEST PASSED" << std::endl << std::endl;
std::cout << "\nTEST PASSED\n\n";

// 6th test
double_radius = 3;
double_expected = 113.04;
double_volume = math::sphere_volume(double_radius);

std::cout << "VOLUME OF A SPHERE" << std::endl;
std::cout << "Input Radius: " << double_radius << std::endl;
std::cout << "Expected Output: " << double_expected << std::endl;
std::cout << "Output: " << double_volume << std::endl;
std::cout << "VOLUME OF A SPHERE\n";
std::cout << "Input Radius: " << double_radius;
std::cout << "\nExpected Output: " << double_expected;
std::cout << "\nOutput: " << double_volume;
assert(double_volume == double_expected);
std::cout << "TEST PASSED" << std::endl << std::endl;
std::cout << "\nTEST PASSED\n\n";

// 7th test
double_radius = 5;
double_height = 2;
double_expected = 157;
double_volume = math::cylinder_volume(double_radius, double_height);

std::cout << "VOLUME OF A CYLINDER" << std::endl;
std::cout << "Input Radius: " << double_radius << std::endl;
std::cout << "Input Height: " << double_height << std::endl;
std::cout << "Expected Output: " << double_expected << std::endl;
std::cout << "Output: " << double_volume << std::endl;
std::cout << "VOLUME OF A CYLINDER\n";
std::cout << "Input Radius: " << double_radius;
std::cout << "\nInput Height: " << double_height;
std::cout << "\nExpected Output: " << double_expected;
std::cout << "\nOutput: " << double_volume;
assert(double_volume == double_expected);
std::cout << "TEST PASSED" << std::endl << std::endl;
std::cout << "\nTEST PASSED\n\nALL TESTS HAVE PASSED\n";
}

/**
Expand Down