How to calculate the 21! (21 factorial) in swift?

后端 未结 6 1054
悲&欢浪女
悲&欢浪女 2020-12-03 11:42

I am making fuction that calculate factorial in swift. like this

func factorial(factorialNumber: UInt64) -> UInt64 {
    if factorialNumber == 0 {
                


        
6条回答
  •  爱一瞬间的悲伤
    2020-12-03 12:35

    Here's a function that accepts any type that conforms to the Numeric protocol, which are all builtin number types.

    func factorial(_ x: N) -> N {
        x == 0 ? 1 : x * factorial(x - 1)
    }
    

提交回复
热议问题