Fibonacci Sequence

by Emanuele 4/15/2008 1:37:00 PM

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

 

Tags:

Open Source

How to measure elapsed time in c# with StopWatch

by Emanuele 2/20/2008 9:54:00 AM

A StopWatch class can measure elapsed time for one interval.
We can use for measuring performance of the new code blocks or algorithms.

Use IsRunning method to determine the state of StopWatch object.
Use Start method to begin measuring elapsed time, and Stop method to stop measuring elapsed time.

        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            textBox1.Text = DateTime.Now.TimeOfDay.ToString();
            Application.DoEvents();

            // Perform a long process
            Thread.Sleep(34564);

            stopwatch.Stop();
            textBox2.Text = DateTime.Now.TimeOfDay.ToString();
            textBox3.Text = stopwatch.Elapsed.Milliseconds.ToString();
            Application.DoEvents();
        }

 

You can find an example application in the attached file.



Powered by BlogEngine.NET 1.3.1.0
Theme by Emanuele Bartolesi

About the author

Name of author Emanuele Bartolesi
I'm a senior developer and project manager.

Contact me Contact me

Calendar

<<  December 2008  >>
MoTuWeThFrSaSu
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in

Download Day 2008