Created
June 4, 2016 04:12
-
-
Save MarcSteven/42a43577088b0144ba959e8b1429c302 to your computer and use it in GitHub Desktop.
How to create big number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct BigInt { | |
var value:String | |
func multiply(right:BigInt) ->BigInt { | |
var a1 = value.characters.reverse().map {Int(String($0))!} | |
var a2 = right.value.characters.reverse().map {Int(String($0))!} | |
var product = [Int](count:a1.count + a2.count,repeatedValue:0) | |
for iterNumber1 in 0..<a1.count { | |
for iterNumber2 in 0..<a2.count{ | |
let idxIter = iterNumber1 + iterNumber2 | |
product[idxIter] = a1[iterNumber1] * a2[iterNumber2] + (idxIter >= product.count ? 0 :product[idxIter]) | |
if product[idxIter] > 9 { | |
product[idxIter + 1] = (product[idxIter / 10] + (idxIter + 1 >= product.count ? 0 :product[idxIter + 1])) | |
product[idxIter] -= (product[idxIter] / 10) * 10 | |
} | |
} | |
} | |
product = Array(product.reverse()) | |
print(product.count) | |
return BigInt(value:product.map {String($0)}.joinWithSeparator("") ) | |
} | |
} | |
func * (left:BigInt,right:BigInt) ->BigInt {return left.multiply(right)} | |
let int1 = BigInt(value: "32123439888883883838") | |
let int2 = BigInt(value: "439938892929929299299292") | |
let product = int1 * int2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment