TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Implementation of the Karatsuba algorithm for fast multiplication More...
#include <cassert>
#include <cstring>
#include <iostream>
#include <vector>
Go to the source code of this file.
Namespaces | |
namespace | divide_and_conquer |
for std::vector | |
namespace | karatsuba_algorithm |
Functions for the Karatsuba algorithm for fast multiplication implementation. | |
Functions | |
std::string | divide_and_conquer::karatsuba_algorithm::add_strings (std::string first, std::string second) |
Binary addition. | |
std::string | divide_and_conquer::karatsuba_algorithm::safe_substr (const std::string &str, int64_t x1, int64_t x2, int64_t n) |
Wrapper function for substr that considers leading zeros. | |
int64_t | divide_and_conquer::karatsuba_algorithm::karatsuba_algorithm (std::string str1, std::string str2) |
The main function implements Karatsuba's algorithm for fast multiplication. | |
static void | test () |
Self-test implementations. | |
int | main () |
Main function. | |
Implementation of the Karatsuba algorithm for fast multiplication
Given two strings in binary notation we want to multiply them and return the value. Simple approach is to multiply bits one by one which will give the time complexity of around O(n^2). To make it more efficient we will be using Karatsuba algorithm to find the product which will solve the problem O(nlogn) of time.
Definition in file karatsuba_algorithm_for_fast_multiplication.cpp.
std::string divide_and_conquer::karatsuba_algorithm::add_strings | ( | std::string | first, |
std::string | second ) |
Binary addition.
first,the | input string 1 |
second,the | input string 2 |
Definition at line 37 of file karatsuba_algorithm_for_fast_multiplication.cpp.
int64_t divide_and_conquer::karatsuba_algorithm::karatsuba_algorithm | ( | std::string | str1, |
std::string | str2 ) |
The main function implements Karatsuba's algorithm for fast multiplication.
str1 | the input string 1 |
str2 | the input string 2 |
Definition at line 112 of file karatsuba_algorithm_for_fast_multiplication.cpp.
int main | ( | void | ) |
Main function.
Definition at line 181 of file karatsuba_algorithm_for_fast_multiplication.cpp.
std::string divide_and_conquer::karatsuba_algorithm::safe_substr | ( | const std::string & | str, |
int64_t | x1, | ||
int64_t | x2, | ||
int64_t | n ) |
Wrapper function for substr that considers leading zeros.
str,the | binary input string. |
x1,the | substr parameter integer 1 |
x2,the | substr parameter integer 2 |
n,is | the length of the "whole" string: leading zeros + str |
Definition at line 86 of file karatsuba_algorithm_for_fast_multiplication.cpp.
|
static |
Self-test implementations.
Definition at line 151 of file karatsuba_algorithm_for_fast_multiplication.cpp.