"Michael Nemtsev" <ne*****@msn.comwrote in message
news:17***************************@msnews.microsof t.com...
Hello veeru,
A little bit better search
http://www.google.com/codesearch?as_...n&as_lang=c%23
vHi All,
vvCan anyone tell about how to create a FIBONACCI series in VB.Net and
vC#
vvThanks in Advance,
vvVeeru
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_...matics%29))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