Fibonacci calculation in C #

I am trying to calculate the Fibonacci sequence in C # in a very simple way, however, when it comes to higher numbers, it appears and stops working, giving the wrong answers.

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++; } } 

I do it so that I can see how to add numbers by adding them to list 1 at a time.

+4
source share
1 answer

ulong too small for fibonacci. You need to use something more .. NET 4 added BigInteger , which should allow for an arbitrary number size.

For lower versions of .NET, you need to find a third-party implementation of third-party developers

+11
source

All Articles