怎么计算21! (21阶乘)迅速? [英] How to calculate the 21! (21 factorial) in swift?

查看:374
本文介绍了怎么计算21! (21阶乘)迅速?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在迅速地计算出阶乘的函数.像这样

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

func factorial(factorialNumber: UInt64) -> UInt64 {
    if factorialNumber == 0 {
        return 1
    } else {
        return factorialNumber * factorial(factorialNumber - 1)
    }
}

let x = factorial(20)

这个函数可以计算到20.

this fuction can calculate untill 20.

我认为阶乘(21)的值大于UINT64_MAX.

I think factorial(21) value bigger than UINT64_MAX.

然后如何计算21! (21阶乘)迅速?

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

推荐答案

无符号64位整数的最大值为18,446,744,073,709,551,615.虽然21! = 51,090,942,171,709,440,000.对于这种情况,您需要一个Big Integer类型.我在Swift中发现了一个关于Big Integer的问题.该链接中有一个用于Big Integer的库.

Unsigned 64 bit integer has a maximum value of 18,446,744,073,709,551,615. While 21! = 51,090,942,171,709,440,000. For this kind of case, you need a Big Integer type. I found a question about Big Integer in Swift. There's a library for Big Integer in that link.

与Swift中的BigInteger等效吗?

这篇关于怎么计算21! (21阶乘)迅速?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆