For a strange reason that I don't know, someone ask to me to implement in a financial application the Fibonacci Sequence.
First of all, I don't know this mathematical role, because I hate this subject!
I seek some information about Fibonacci and this role over Internet and the best information that I found is on wikipedia.
There is a simple way to find and display the number of Fibonacci sequence in C#.
Here, I write the function in C#:
class Program
{
static double FibonacciSequence(double x)
{
if (x <= 1) return 1;
return FibonacciSequence(x - 1) + FibonacciSequence(x - 2);
}
static void Main()
{
for (int i = 0; i <= 40; i++)
{
Console.WriteLine("Fibonacci number: {0}", FibonacciSequence(i));
}
Console.WriteLine("Finished.");
Console.ReadKey();
}
}
and in Vb.net:
Class Program
Private Shared Function FibonacciSequence(ByVal x As Double) As Double
If x <= 1 Then
Return 1
End If
Return FibonacciSequence(x - 1) + FibonacciSequence(x - 2)
End Function
Private Shared Sub Main()
For i As Integer = 0 To 40
Console.WriteLine("Fibonacci number: {0}", FibonacciSequence(i))
Next
Console.WriteLine("Finished.")
Console.ReadKey()
End Sub
End Class