Created
January 16, 2018 16:45
-
-
Save geor-kasapidi/7e67e79d996a4d5145b318c4fb6dfc98 to your computer and use it in GitHub Desktop.
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
import Foundation | |
func mult(a: [UInt16], b: [UInt16]) -> [UInt16] { | |
var c = [UInt16]() | |
for (i, x) in b.reversed().enumerated() { | |
var r = UInt16(0) | |
for (j, y) in a.reversed().enumerated() { | |
let k = i + j | |
if k < c.count { | |
r += c[k] | |
} | |
let z = x * y + r | |
if k < c.count { | |
c[k] = z % 10 | |
} else { | |
c.append(z % 10) | |
} | |
r = z / 10 | |
} | |
while r > 0 { | |
c.append(r % 10) | |
r = r/10 | |
} | |
} | |
return c.reversed() | |
} | |
func fact(n: Int) -> [UInt16] { | |
var res: [UInt16] = [1] | |
if n < 2 { | |
return res | |
} | |
for x in 1...UInt16(n) { | |
res = mult(a: res, b: [x]) | |
} | |
return res | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment