diff --git a/bit_manipulation/count_bits_flip.cpp b/bit_manipulation/count_bits_flip.cpp index 2ab2ce31c1..29dab3c969 100644 --- a/bit_manipulation/count_bits_flip.cpp +++ b/bit_manipulation/count_bits_flip.cpp @@ -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); @@ -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 diff --git a/math/factorial.cpp b/math/factorial.cpp index e350df6bdf..f3d3d2ade8 100644 --- a/math/factorial.cpp +++ b/math/factorial.cpp @@ -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 diff --git a/math/magic_number.cpp b/math/magic_number.cpp index c07869e3df..7a6417c78f 100644 --- a/math/magic_number.cpp +++ b/math/magic_number.cpp @@ -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 @@ -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"; } /** diff --git a/math/volume.cpp b/math/volume.cpp index 87bfa6a825..07f6ed15d2 100644 --- a/math/volume.cpp +++ b/math/volume.cpp @@ -25,7 +25,7 @@ namespace math { * @returns The volume of the cube */ template -T cube_volume(T length) { +constexpr inline T cube_volume(T length) noexcept { return std::pow(length, 3); } @@ -38,7 +38,7 @@ T cube_volume(T length) { * @returns The volume of the rectangular prism */ template -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; } @@ -50,7 +50,7 @@ T rect_prism_volume(T length, T width, T height) { * @returns The volume of the cone */ template -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; } @@ -64,7 +64,7 @@ T cone_volume(T radius, T height, double PI = 3.14) { * @returns The volume of the triangular prism */ template -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; } @@ -77,7 +77,7 @@ T triangle_prism_volume(T base, T height, T depth) { * @returns The volume of the pyramid */ template -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; } @@ -88,7 +88,7 @@ T pyramid_volume(T length, T width, T height) { * @returns The volume of the sphere */ template -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; } @@ -100,7 +100,7 @@ T sphere_volume(T radius, double PI = 3.14) { * @returns The volume of the cylinder */ template -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 @@ -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 @@ -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; @@ -146,14 +146,14 @@ 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; @@ -161,13 +161,13 @@ static void test() { 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; @@ -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; @@ -192,26 +192,26 @@ 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; @@ -219,13 +219,13 @@ static void test() { 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"; } /**