re: Daily Challenge #7 - Factorial Decomposition VIEW POST

FULL DISCUSSION
 

My solution in Swift, factorial numbers are too big for Int but it's really bored to deal with NSDecimalNumber :

extension Int {
    var factorial: UInt64 {
        (1...self).reduce(into: UInt64(1)) { $0 *= UInt64($1) }
    }

    func numberOfDivision(with number: Int) -> Int {
        guard number != 1 else { return 1 }
        var loop = 0
        var value = self.factorial
        while value % UInt64(number) == 0 {
            value /= UInt64(number)
            loop += 1
        }
        return loop
    }

    var primeNumbers: [Int] {
        (1..<self).filter { number in
            number > 1 && !(2..<number).contains { number % $0 == 0 }
        }
    }
}

func decomp(factorial: Int) -> String {
    factorial.primeNumbers.reduce(into: [String]()) {
        let numberOfDivision = factorial.numberOfDivision(with: $1)
        return $0.append("\($1)\(numberOfDivision == 1 ? "" : "^\(numberOfDivision)")")
    }.joined(separator: " * ")
}
code of conduct - report abuse