在C#中计算Fibonacci

Mic*_*cia 5 c# fibonacci ulong

我试图以一种非常简单的方式计算C#中的Fibonacci序列,但是当涉及到更高的数字时,它会通过给出错误的答案来阻止并停止工作.

ulong num = 1;
ulong lnum = 0;
uint x = 1;

private void Form1_Load(object sender, EventArgs e)
{
    listBox1.Items.Add("(0) " + 1);
}

private void timer1_Tick(object sender, EventArgs e)
{
    if (x <= 1000)
    {
        ulong newnum = lnum + num;
        listBox1.Items.Add("(" + x + ") " + newnum);
        listBox1.SetSelected((int)x, true);
        lnum = num;
        num = newnum;
        x++;
     }
}
Run Code Online (Sandbox Code Playgroud)

我正在通过一种方式制作它,我可以通过一次将它们添加到列表框1来添加数字.

Eup*_*ric 11

ulong对于斐波纳契来说太小了.你需要使用更大的东西..NET 4添加了BigInteger,它应允许任意数量的大小.

对于较低的.NET版本,您需要找到similliar第三方实现