Skip to content

Instantly share code, notes, and snippets.

@tikipatel
Created March 21, 2019 19:37
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 tikipatel/fcc3fe0edfa11ad79cf1b6383269c761 to your computer and use it in GitHub Desktop.
Save tikipatel/fcc3fe0edfa11ad79cf1b6383269c761 to your computer and use it in GitHub Desktop.
Prints out steps of multiplicative persistence
func multiplicativePersistence(of int: UInt64, step: Int = 1) {
if step == 1 {
print("Step 0:", int)
}
var mutatingInt = int
var remainder: UInt64
var currentValue: UInt64 = 1
while mutatingInt != 0 {
(mutatingInt, remainder) = mutatingInt.quotientAndRemainder(dividingBy: 10)
currentValue *= UInt64(remainder)
}
print("Step \(step):", currentValue)
if currentValue.quotientAndRemainder(dividingBy: 10).quotient == 0 {
return
} else {
multiplicativePersistence(of: currentValue, step: step+1)
}
}
multiplicativePersistence(of: 888888)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment