Created
March 21, 2019 19:37
-
-
Save tikipatel/fcc3fe0edfa11ad79cf1b6383269c761 to your computer and use it in GitHub Desktop.
Prints out steps of multiplicative persistence
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
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