-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKaratsubaIntegerMultiplication.cpp
More file actions
39 lines (30 loc) · 1.1 KB
/
Copy pathKaratsubaIntegerMultiplication.cpp
File metadata and controls
39 lines (30 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//============================================================================
// Name : KaratsubaIntegerMultiplication.cpp
// Author : Almgwary
// Date : 21-May-2015
//============================================================================
#include <bits/stdc++.h>
using namespace std;
unsigned _power( unsigned val, unsigned _pow=0 ) {
if ( _pow <= 0 )
return 1;
return val * _power( val, _pow-1 );
}
unsigned KaratsubaIntegerMultiplication (unsigned x , unsigned y , unsigned N ){
if(N < 2) {
return x*y ;
}
unsigned tenPowerN = _power(10,N) , tenPowerN2 = _power(10,N/2) ;
unsigned a = x/tenPowerN2 , b = x%tenPowerN2 ,
c = y/tenPowerN2 , d = y%tenPowerN2 ;
unsigned ac = KaratsubaIntegerMultiplication(a ,c , N/2) ,
bd = KaratsubaIntegerMultiplication(b ,d , N/2) ,
abPcd = KaratsubaIntegerMultiplication(a+b , c+d , N/2 );
unsigned adPbc = abPcd - ac - bd ;
return tenPowerN*ac + tenPowerN2*adPbc + bd ;
}
int main()
{
cout << KaratsubaIntegerMultiplication(5678 ,1234 , 4) << endl;
return 0;
}