|
1 | 1 | //: Playground - noun: a place where people can play
|
2 | 2 |
|
3 | 3 | import Foundation
|
4 |
| - |
| 4 | +// last checked with Xcode 9.0b4 |
| 5 | +#if swift(>=4.0) |
| 6 | +print("Hello, Swift 4") |
| 7 | +#endif |
5 | 8 | precedencegroup ExponentiativePrecedence {
|
6 |
| - higherThan: MultiplicationPrecedence |
7 |
| - lowerThan: BitwiseShiftPrecedence |
8 |
| - associativity: left |
| 9 | + higherThan: MultiplicationPrecedence |
| 10 | + lowerThan: BitwiseShiftPrecedence |
| 11 | + associativity: left |
9 | 12 | }
|
10 | 13 |
|
11 | 14 | infix operator ^^: ExponentiativePrecedence
|
12 | 15 | func ^^ (radix: Int, power: Int) -> Int {
|
13 |
| - return Int(pow(Double(radix), Double(power))) |
| 16 | + return Int(pow(Double(radix), Double(power))) |
14 | 17 | }
|
15 | 18 |
|
16 | 19 | // Long Multiplication - O(n^2)
|
17 | 20 | func multiply(_ num1: Int, by num2: Int, base: Int = 10) -> Int {
|
18 |
| - let num1Array = String(num1).characters.reversed().map { Int(String($0))! } |
19 |
| - let num2Array = String(num2).characters.reversed().map { Int(String($0))! } |
20 |
| - |
21 |
| - var product = Array(repeating: 0, count: num1Array.count + num2Array.count) |
22 |
| - |
23 |
| - for i in num1Array.indices { |
24 |
| - var carry = 0 |
25 |
| - for j in num2Array.indices { |
26 |
| - product[i + j] += carry + num1Array[i] * num2Array[j] |
27 |
| - carry = product[i + j] / base |
28 |
| - product[i + j] %= base |
| 21 | + let num1Array = String(num1).characters.reversed().map { Int(String($0))! } |
| 22 | + let num2Array = String(num2).characters.reversed().map { Int(String($0))! } |
| 23 | + |
| 24 | + var product = Array(repeating: 0, count: num1Array.count + num2Array.count) |
| 25 | + |
| 26 | + for i in num1Array.indices { |
| 27 | + var carry = 0 |
| 28 | + for j in num2Array.indices { |
| 29 | + product[i + j] += carry + num1Array[i] * num2Array[j] |
| 30 | + carry = product[i + j] / base |
| 31 | + product[i + j] %= base |
| 32 | + } |
| 33 | + product[i + num2Array.count] += carry |
29 | 34 | }
|
30 |
| - product[i + num2Array.count] += carry |
31 |
| - } |
32 |
| - |
33 |
| - return Int(product.reversed().map { String($0) }.reduce("", +))! |
| 35 | + |
| 36 | + return Int(product.reversed().map { String($0) }.reduce("", +))! |
34 | 37 | }
|
35 | 38 |
|
36 | 39 | // Karatsuba Multiplication - O(n^log2(3))
|
37 | 40 | func karatsuba(_ num1: Int, by num2: Int) -> Int {
|
38 |
| - let num1Array = String(num1).characters |
39 |
| - let num2Array = String(num2).characters |
40 |
| - |
41 |
| - guard num1Array.count > 1 && num2Array.count > 1 else { |
42 |
| - return multiply(num1, by: num2) |
43 |
| - } |
44 |
| - |
45 |
| - let n = max(num1Array.count, num2Array.count) |
46 |
| - let nBy2 = n / 2 |
47 |
| - |
48 |
| - let a = num1 / 10^^nBy2 |
49 |
| - let b = num1 % 10^^nBy2 |
50 |
| - let c = num2 / 10^^nBy2 |
51 |
| - let d = num2 % 10^^nBy2 |
52 |
| - |
53 |
| - let ac = karatsuba(a, by: c) |
54 |
| - let bd = karatsuba(b, by: d) |
55 |
| - let adPlusbc = karatsuba(a+b, by: c+d) - ac - bd |
56 |
| - |
57 |
| - let product = ac * 10^^(2 * nBy2) + adPlusbc * 10^^nBy2 + bd |
58 |
| - |
59 |
| - return product |
| 41 | + let num1Array = String(num1).characters |
| 42 | + let num2Array = String(num2).characters |
| 43 | + |
| 44 | + guard num1Array.count > 1 && num2Array.count > 1 else { |
| 45 | + return multiply(num1, by: num2) |
| 46 | + } |
| 47 | + |
| 48 | + let n = max(num1Array.count, num2Array.count) |
| 49 | + let nBy2 = n / 2 |
| 50 | + |
| 51 | + let a = num1 / 10^^nBy2 |
| 52 | + let b = num1 % 10^^nBy2 |
| 53 | + let c = num2 / 10^^nBy2 |
| 54 | + let d = num2 % 10^^nBy2 |
| 55 | + |
| 56 | + let ac = karatsuba(a, by: c) |
| 57 | + let bd = karatsuba(b, by: d) |
| 58 | + let adPlusbc = karatsuba(a+b, by: c+d) - ac - bd |
| 59 | + |
| 60 | + let product = ac * 10^^(2 * nBy2) + adPlusbc * 10^^nBy2 + bd |
| 61 | + |
| 62 | + return product |
60 | 63 | }
|
0 commit comments