From 402bf770f5eee684c043075f714ebcc6a6a88b28 Mon Sep 17 00:00:00 2001 From: newt Date: Wed, 9 Oct 2024 18:02:42 +0100 Subject: [PATCH] feat(c++): karatsuba algorithm - --- languages/c++/CMakeLists.txt | 1 + languages/c++/code/karatsuba.cc | 45 +++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 languages/c++/code/karatsuba.cc diff --git a/languages/c++/CMakeLists.txt b/languages/c++/CMakeLists.txt index 5ffd049..c10e694 100644 --- a/languages/c++/CMakeLists.txt +++ b/languages/c++/CMakeLists.txt @@ -8,3 +8,4 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) add_executable(pascal code/pascal.cc) add_executable(babylonian code/babylonian.cc) +add_executable(karatsuba code/karatsuba.cc) diff --git a/languages/c++/code/karatsuba.cc b/languages/c++/code/karatsuba.cc new file mode 100644 index 0000000..4bb8953 --- /dev/null +++ b/languages/c++/code/karatsuba.cc @@ -0,0 +1,45 @@ +#include +#include +using namespace std; + +double karatsuba(double num1, double num2) { + int num1Length = to_string((int)num1).size(); + int num2Length = to_string((int)num2).size(); + + // Fallback to traditional multiplication + if (num1Length == 1 || num2Length == 1) { + return num1 * num2; + } else { + double n = floor(max(num1Length, num2Length) / 2); + + double lowNum1 = fmod(num1, pow(10, n)); + double lowNum2 = fmod(num2, pow(10, n)); + + double highNum1 = floor(num1 / pow(10, n)); + double highNum2 = floor(num2 / pow(10, n)); + + double z0 = karatsuba(lowNum1, lowNum2); + double z1 = karatsuba(lowNum1 + highNum1, lowNum2 + highNum2); + double z2 = karatsuba(highNum1, highNum2); + + return (z2 * pow(10, n * 2)) + ((z1 - z2 - z0) * pow(10, n)) + z0; + } +} + +int main() { + cout << "Give me a number! "; + + double num1; + cin >> num1; + + cout << "Give me a number to multiply " + to_string(num1) + " by! "; + + double num2; + cin >> num2; + + double karatsubaResult = karatsuba(num1, num2); + double quadraticResult = num1 * num2; + + cout << endl << "Quadratic Result: " + to_string(quadraticResult) << endl; + cout << "Karatsuba Result: " + to_string(karatsubaResult); +}