Skip to content

Instantly share code, notes, and snippets.

@MarcSteven
Created June 4, 2016 04:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarcSteven/42a43577088b0144ba959e8b1429c302 to your computer and use it in GitHub Desktop.
Save MarcSteven/42a43577088b0144ba959e8b1429c302 to your computer and use it in GitHub Desktop.
How to create big number
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