From d937085f049800c8daade51433bbdaab2cc96b0e Mon Sep 17 00:00:00 2001 From: fdai7930 Date: Thu, 8 Feb 2024 02:00:09 +0100 Subject: [PATCH] multiplication recursive optimized --- src/multiplication.c | 12 ++++++++++++ src/multiplication.h | 1 + 2 files changed, 13 insertions(+) diff --git a/src/multiplication.c b/src/multiplication.c index 9512607..5262de3 100644 --- a/src/multiplication.c +++ b/src/multiplication.c @@ -138,6 +138,18 @@ int multiply_using_differences(int num1, int num2) { return (sum - difference) * difference / 4; } +int multiply_recursive_optimized(int num1, int num2) { + if (num2 == 0) { + return 0; + } + int result = multiply_recursive_optimized(num1, num2 >> 1); + result <<= 1; + if (num2 & 1) { + result += num1; + } + return result; +} + diff --git a/src/multiplication.h b/src/multiplication.h index c2b8540..1524c22 100644 --- a/src/multiplication.h +++ b/src/multiplication.h @@ -21,6 +21,7 @@ int multiply_by_prime_and_its_square(int num, int prime); int multiply_by_odd(int num, int factor); int multiply_using_binary_enumeration(int num1, int num2); int multiply_using_differences(int num1, int num2); +int multiply_recursive_optimized(int num1, int num2); #endif // MULTIPLICATION_H