FIBONACCI

  • Thread starter Thread starter veeru
  • Start date Start date
V

veeru

Hi All,

Can anyone tell about how to create a FIBONACCI series in VB.Net and C#

Thanks in Advance,

Veeru
 
Michael Nemtsev said:
Hello veeru,

A little bit better search
http://www.google.com/codesearch?as_q=Fibonacci&btnG=Search+Code&hl=en&as_lang=c#

v> Hi All,
v> v> Can anyone tell about how to create a FIBONACCI series in VB.Net and
v> C#
v> v> Thanks in Advance,
v> v> Veeru
v>

While those are correct implementations of the fibonacci sequence (it's a
sequence (http://en.wikipedia.org/wiki/Sequence), not a
series(http://en.wikipedia.org/wiki/Series_(mathematics)))the vast
majority are recursive implementations, that because of the limited stack
space, only work for the first few numbers in the sequence, and perform
really poorly.


Here's a little sample with a recursive and an iterative implementation of
fibonacci. Play around with it so see how much better the iterative
implementation is.


class Program
{
static int FibR(int n)
{
if (n < 2)
return 1;
else
return FibR(n - 1) + FibR(n - 2);
}
static long FibI(long n)
{
if (n < 2)
return 1;

long fp = 1;
long fpp = 1;
long f = 0;
for (int i = 2; i < n; i++)
{
f = fp + fpp;
fpp = fp;
fp = f;
}
if (f < 0)
throw new OverflowException();

return f;
}

public static int Main(string[] args)
{
Console.WriteLine(FibI(1000 * 1000 * 1000));
Console.ReadKey();
return 0;
}
}


David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top