c# - 大于 ULong 的值?计算100!

标签 c# asp.net vb.net math

我正在尝试计算 100!而且似乎没有内置的阶乘函数。所以,我写了:

Protected Sub ComputeFactorial(ByVal n As ULong)
        Dim factorial As ULong = 1
        Dim i As Integer
        For i = 1 To n
            factorial = factorial * i
        Next
        lblAnswer.Text = factorial
    End Sub

不幸的是,以 100 的值为 n rresults in

Value was either too large or too small for a UInt64.

那么,有没有更大的数据类型来存放数字呢?我的方法有误吗?我很无助吗?

最佳答案

听起来像欧拉计划。

.NET 4.0 有 System.Numerics.BigInteger,或者你可以在这里找到一个非常棒的实现:
C# BigInteger Class

编辑:树 :(

我要补充 - CodeProject 上的版本具有其他功能,例如整数平方根、素数测试、Lucas 序列生成。此外,您无法直接访问 .NET 实现中的缓冲区,这对我尝试的一些事情来说很烦人。

关于c# - 大于 ULong 的值?计算100!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1267583/

相关文章:

c# - ASP.NET 客户端验证与服务器端验证

c# - 向下滚动时加载 GridView 数据

java - 如何在 Java Web 服务中实现 Soap 错误?

c# - AES256 解密损坏第一个字节

c# - Visual Studio 2019 : Intellisense does not recognize assemblies as referenced

c# - SQL(本地)不起作用

asp.net - 使用 PostedFile.InputStream 两次

c# - OPC UA基础SDK : Server does not have an instance certificate assigned

javascript - 当对话框在 IE11 中再次出现时按钮不会更改为禁用

asp.net - 选择文本框后,如何选择单选按钮?