Skip to content

Instantly share code, notes, and snippets.

@bugrym
Created February 21, 2020 08:50
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 bugrym/3d81f4e0c73007894cfddd9928ff1a4b to your computer and use it in GitHub Desktop.
Save bugrym/3d81f4e0c73007894cfddd9928ff1a4b to your computer and use it in GitHub Desktop.
1281. Subtract the Product and Sum of Digits of an Integer
func subtractProductAndSum(_ n: Int) -> Int {
var givenNum = n
var num:[Int] = []
if n >= 1 && n <= 100000 {
repeat {
let digit = givenNum%10
givenNum /= 10
num.append(digit)
} while (givenNum != 0)
let product = num.map({$0}).reduce(1, *)
let sum = num.map({$0}).reduce(0, +)
return product - sum
}
return 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment