From 5dc21dee2380d3f29231331646e645a5c67929d7 Mon Sep 17 00:00:00 2001 From: vi Date: Mon, 5 Oct 2026 17:01:56 +0300 Subject: [PATCH 1/5] edit bit_manipulation/count_bits_flip.cpp --- bit_manipulation/count_bits_flip.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/bit_manipulation/count_bits_flip.cpp b/bit_manipulation/count_bits_flip.cpp index 2ab2ce31c1..c555f2d74c 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,9 @@ 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'; + // using '\n' instead of std::endl to omit the output buffer + // from being flushed at the end of the function } /** * @brief Main function From 4fc7e858d0b55352cc02eb5b14f1836bdb6efd1c Mon Sep 17 00:00:00 2001 From: vi Date: Tue, 6 Oct 2026 06:25:35 +0300 Subject: [PATCH 2/5] Update bit_manipulation/count_bits_flip.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- bit_manipulation/count_bits_flip.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/bit_manipulation/count_bits_flip.cpp b/bit_manipulation/count_bits_flip.cpp index c555f2d74c..29dab3c969 100644 --- a/bit_manipulation/count_bits_flip.cpp +++ b/bit_manipulation/count_bits_flip.cpp @@ -79,9 +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!" << '\n'; - // using '\n' instead of std::endl to omit the output buffer - // from being flushed at the end of the function + std::cout << "All test cases successfully passed!\n"; } /** * @brief Main function From d8823fcaa342515361c56760847922eaa33130c1 Mon Sep 17 00:00:00 2001 From: vi Date: Tue, 6 Oct 2026 07:23:43 +0300 Subject: [PATCH 3/5] edit math/factorial.cpp to use a for loop instead of recursion --- math/factorial.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) 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 From 91487bde5615da307c1175e8599b2cf51f642021 Mon Sep 17 00:00:00 2001 From: vi Date: Tue, 6 Oct 2026 07:38:02 +0300 Subject: [PATCH 4/5] edit magic_number.cpp: replace if statements with a single return statement make a function constexpr and inline replace std::endl with \n in volume.cpp --- math/magic_number.cpp | 16 ++------ math/volume.cpp | 86 +++++++++++++++++++++---------------------- 2 files changed, 47 insertions(+), 55 deletions(-) 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..8abe3c8179 100644 --- a/math/volume.cpp +++ b/math/volume.cpp @@ -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"; } /** From b8581ad81c0d05979cd2af0db9289fe42934a0aa Mon Sep 17 00:00:00 2001 From: vi Date: Tue, 6 Oct 2026 07:42:37 +0300 Subject: [PATCH 5/5] mark every function in volume.cpp with 'constexpr', 'inline' and 'noexcept' --- math/volume.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/math/volume.cpp b/math/volume.cpp index 8abe3c8179..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