Karatsuba algorithm C++ implementation
来源:互联网 发布:sql团体培训 编辑:程序博客网 时间:2024/05/21 21:49
//// main.cpp// multiply//// Created by Longxiang Lyu on 5/23/16.// Copyright (c) 2016 Leo Lyu. All rights reserved.//// the karatsuba's algorithm only needs O(n^log3) bit operation to do the// multiplication of two integers, which is better than O(n^2) solutions#include <iostream>#include <string>using namespace std;size_t equalizeLength(string &s1, string &s2){ size_t len1 = s1.size(), len2 = s2.size(); if (len1 < len2) { for (int i = 0; i < len2 - len1; ++i) s1 = '0' + s1; return len2; } else if (len1 > len2) { for (int i = 0; i < len1 - len2; ++i) s2 = '0' + s2; } return len1;}string strAddition(string s1, string s2){ string ret; int carry = 0; size_t len = equalizeLength(s1, s2); for (int i = len - 1; i >= 0; --i) { int firstBit = s1.at(i) - '0'; int secondBit = s2.at(i) - '0'; int sum = (firstBit ^ secondBit ^ carry) + '0'; ret = static_cast<char>(sum) + ret; carry = (firstBit & secondBit) | (firstBit & carry) | (secondBit & carry); } if (carry) ret = '1' + ret; return ret;}long int Karatsuba_multiply(string s1, string s2){ size_t len = equalizeLength(s1, s2); // base case if (len == 0) return 0; if (len == 1) return (s1[0] - '0') * (s2[0] - '0'); size_t floor = len / 2; size_t ceil = len - floor; string a = s1.substr(0, floor); string b = s1.substr(floor, ceil); string c = s2.substr(0, floor); string d = s2.substr(floor, ceil); long int p1 = Karatsuba_multiply(a, c); long int p2 = Karatsuba_multiply(b, d); long int p3 = Karatsuba_multiply(strAddition(a, b), strAddition(c, d)); return (1<<(2 * ceil)) * p1 + (1<<(ceil)) * (p3 - p1 - p2) + p2;}int main(int argc, const char * argv[]) { string s1 = "10101010"; string s2 = "111101"; cout << s1 << " multiplies " << s2 << " is " << Karatsuba_multiply(s1, s2) << endl; return 0;}
Reference:
https://en.wikipedia.org/wiki/Karatsuba_algorithm
http://www.geeksforgeeks.org/divide-and-conquer-set-2-karatsuba-algorithm-for-fast-multiplication/
http://www.cs.princeton.edu/~wayne/kleinberg-tardos/pdf/05DivideAndConquerII.pdf
0 0
- Karatsuba algorithm C++ implementation
- Karatsuba algorithm乘法问题
- Algorithm: The implementation of Trie Tree (C++)
- Rob Hess's C implementation of SIFT algorithm
- FFT Algorithm Implementation
- Lempel-Ziv Algorithm's Implementation
- Very simple A* algorithm implementation
- Java MD5 Encryption Algorithm Implementation
- TEA Algorithm Implementation on iOS
- [Sonia] Implementation of Huffman Algorithm
- Implementation codes of Data Structures and Algorithm Analysis in C (1)
- implementation of General Sort Algorithm - mark
- Dijkstra's Shortest-Path Algorithm Implementation(TODO)
- MST implementation with Kruskal and Boruvka algorithm
- Implementation Of TDOA Using Chan Algorithm And Taylor Algorithm
- objective-c MD5 Implementation
- karatsuba乘法
- Karatsuba 乘法
- LeetCode 351. Android Unlock Patterns(安卓解锁)
- 【设计模式】创建型模式之生成器Builder
- JavaScript概览(一)--基本语法
- [BZOJ1009][HNOI2008]GT考试(AC自动机+dp+矩阵优化)
- linux 学习笔记(三):open、creat、close 函数的使用,文件的创建、打开与关闭
- Karatsuba algorithm C++ implementation
- Laravel 完整开源项目大全
- linux 系统
- vector::erase方法,返回值是更新后的容器的被删除元素的后一个元素的迭代器,如果需要指向删除元素的前面一个元素的迭代器就要-1
- 大整数乘法
- CSS选择器
- memento
- 金融tag对照表
- IOS中UITableViewCell使用详解