c# - 生成贝尔数算法

标签 c# algorithm

我正在尝试为 500 到 1000 之间的大值生成第 n 个铃声。

Bells 数量巨大,我无法将其存储在 ulong 中。

(要知道铃声号码:http://en.wikipedia.org/wiki/Bell_number)

我尝试了三角法来计算这个数字。

能不能想出一种方法来保存像 dat 这样的大数字并执行操作。

这是我写的代码


using System;

class Program    
{        
    static void Main(string[] args)
    {
        int length;
        do
        {
            length =-1;                
            string numLength= Console.ReadLine();
            if (int.TryParse(numLength, out length))
            {   
                Console.WriteLine("Sequence length is : {0}",
                                           TriangularMethod(length));
            }
        }while(length>0);
    }

    static ulong TriangularMethod(int n)
    {
        Dictionary<long, List<ulong>> triangle = 
                                new Dictionary<long, List<ulong>>();
        triangle.Add(1, new List<ulong>(new ulong[] { 1 }));

        for (int i = 2; i <= n; i++)
        {
            triangle.Add(i, new List<ulong>());
            triangle[i].Add(triangle[i - 1].Last());
            ulong lastVal = 0;
            for (int k = 1; k < i; k++)
            {
                lastVal = triangle[i][k - 1] + triangle[i - 1][k - 1];
                triangle[i].Add(lastVal);                    
            }

            triangle.Remove(i - 2);
        }
        return triangle[n].Last();
    }        
}

如果有更快的计算方法。请包容。

最佳答案

关于c# - 生成贝尔数算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10102653/

相关文章:

c# - "Control with id could not be located or a different control is assigned to the same ID after postback."错误

c# - 5 个字符的 RegEx 匹配序列

c# - SignalR 调用问题

algorithm - 搜索多个值的索引的算法是什么?

c# - 如何在 C# 中实现点在凸多边形算法中的有效测试?

c# - 可重复的复杂正则表达式,带点 '.' 分隔符

c# - 在 ASP.NET MVC 中支持 WCF

python - 遗传算法不起作用

python - 我的扩展欧几里德算法(python)有什么问题?

Java实现的调度算法