forked from kodecocodes/swift-algorithm-club
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKaratsubaMultiplication.swift
45 lines (35 loc) · 994 Bytes
/
KaratsubaMultiplication.swift
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
40
41
42
43
44
45
//
// KaratsubaMultiplication.swift
//
//
// Created by Richard Ash on 9/12/16.
//
//
import Foundation
precedencegroup ExponentiativePrecedence {
higherThan: MultiplicationPrecedence
lowerThan: BitwiseShiftPrecedence
associativity: left
}
infix operator ^^: ExponentiativePrecedence
func ^^ (radix: Int, power: Int) -> Int {
return Int(pow(Double(radix), Double(power)))
}
func karatsuba(_ num1: Int, by num2: Int) -> Int {
let num1Array = String(num1).characters
let num2Array = String(num2).characters
guard num1Array.count > 1 && num2Array.count > 1 else {
return num1 * num2
}
let n = max(num1Array.count, num2Array.count)
let nBy2 = n / 2
let a = num1 / 10^^nBy2
let b = num1 % 10^^nBy2
let c = num2 / 10^^nBy2
let d = num2 % 10^^nBy2
let ac = karatsuba(a, by: c)
let bd = karatsuba(b, by: d)
let adPlusbc = karatsuba(a+b, by: c+d) - ac - bd
let product = ac * 10^^(2 * nBy2) + adPlusbc * 10^^nBy2 + bd
return product
}