@@ -6,58 +6,58 @@ import Foundation
6
6
print ( " Hello, Swift 4 " )
7
7
#endif
8
8
precedencegroup ExponentiativePrecedence {
9
- higherThan : MultiplicationPrecedence
10
- lowerThan : BitwiseShiftPrecedence
11
- associativity : left
9
+ higherThan : MultiplicationPrecedence
10
+ lowerThan : BitwiseShiftPrecedence
11
+ associativity : left
12
12
}
13
13
14
14
infix operator ^^ : ExponentiativePrecedence
15
15
func ^^ ( radix: Int , power: Int ) -> Int {
16
- return Int ( pow ( Double ( radix) , Double ( power) ) )
16
+ return Int ( pow ( Double ( radix) , Double ( power) ) )
17
17
}
18
18
19
19
// Long Multiplication - O(n^2)
20
20
func multiply( _ num1: Int , by num2: Int , base: Int = 10 ) -> Int {
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
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
34
32
}
35
-
36
- return Int ( product. reversed ( ) . map { String ( $0) } . reduce ( " " , + ) ) !
33
+ product [ i + num2Array. count] += carry
34
+ }
35
+
36
+ return Int ( product. reversed ( ) . map { String ( $0) } . reduce ( " " , + ) ) !
37
37
}
38
38
39
39
// Karatsuba Multiplication - O(n^log2(3))
40
40
func karatsuba( _ num1: Int , by num2: Int ) -> Int {
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
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
63
63
}
0 commit comments