DEV Community

loading...

Discussion on: Daily Challenge #214 - Persistent Bugger

Collapse
kesprit profile image
kesprit

My Swift solution :

extension BinaryInteger {
    var digits: [Int] {
        String(describing: self).compactMap { Int(String($0)) }
    }

    var persistence: Int {
        self.digits.reduce(into: 1) { $0 = $0 * $1 }
    }
}

func persistence(number: Int) -> Int {
    number > 9 ? 1 + persistence(number: number.persistence) : 0
}


persistence(number: 4) // Output: 0
persistence(number: 39) // Output: 3
persistence(number: 999) // Output: 4
persistence(number: 5) // Output: 0
persistence(number: 52) // Output: 2
persistence(number: 377) // Output: 4