Skip to content

Instantly share code, notes, and snippets.

@kebunit
Created February 26, 2016 10:41
Show Gist options
  • Save kebunit/4fa83273090837fc1157 to your computer and use it in GitHub Desktop.
Save kebunit/4fa83273090837fc1157 to your computer and use it in GitHub Desktop.
FAST C/C++ Implementation of the Karatsuba Multiplication algorithm. This is the only C++ implementation that I found online that uses straight C++ primitives to store data instead of std::vector or std::string objects. Because of this, it's pretty speedy. You can use this file in your program
//============================================================================
// Name : KaratsubaMultiplication.cpp
// Author : Evin Ugur (http://evinugur.com)
// Version : 1.0
// Copyright : Copyright 2015. You can use this code however and wherever you want no strings attached
// Description : C++ Functions to Perform Karatsbuba Multiplications
//============================================================================
#include <iostream>
#include <math.h>
#include "KaratsubaMultiplication.h"
using namespace std;
int getLength(long long value) {
int counter = 0;
while (value != 0) {
counter++;
value /= 10;
}
return counter;
}
long long multiply(long long x, long long y) {
int xLength = getLength(x);
int yLength = getLength(y);
// the bigger of the two lengths
int N = (int)(fmax(xLength, yLength));
// if the max length is small it's faster to just flat out multiply the two nums
if (N < 10)
return x * y;
//max length divided and rounded up
N = (N/2) + (N%2);
long long multiplier = pow(10, N);
long long b = x/multiplier;
long long a = x - (b * multiplier);
long long d = y / multiplier;
long long c = y - (d * N);
long long z0 = multiply(a,c);
long long z1 = multiply(a + b, c + d);
long long z2 = multiply(b, d);
return z0 + ((z1 - z0 - z2) * multiplier) + (z2 * (long long)(pow(10, 2 * N)));
}
// C++ example (uses cout...)
// (this code works in straight C too though if you use a different main function)
int main() {
// two numbers
long long a = 2406994;
long long b = 2328563;
cout << multiply(a,b) << endl;
return 0;
}
/*
* KaratsubaMultiplication.h
*
* Created on: Feb 4, 2015
* Author: Evin Ugur (http://evinugur.com)
*/
#ifndef KARATSUBAMULTIPLICATION_H_
#define KARATSUBAMULTIPLICATION_H_
int getLength(long long value); // returns the number of digits a number has
long long multiply(long long x, long long y); // Karatsuba multiplication of two numbers
#endif /* KARATSUBAMULTIPLICATION_H_ */
@baohl00
Copy link

baohl00 commented May 6, 2021

In line 33, it should be $N==1$. Because you have got the length of value $x$.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment