TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Fibonacci Sequence

This C# program computes Fibonacci numbers with an iterative algorithm.

Fibonacci numbers are a fascinating sequence.

This sequence models and predicts financial markets and natural phenomena. We can compute Fibonacci numbers with recursion. This can be slow. It is also possible to use iteration.

Example. First, this is an iterative method. Conceptually, an iterative Fibonacci method stores the result of the previous Fibonacci number before computing the next one. This reduces the time required.

Note: To compute a Fibonacci number at a certain position N, we have to loop through all previous numbers starting at position 0.

Based on:

.NET 4.5

C# program that computes Fibonacci iteratively

using System;

class Program
{
    public static int Fibonacci(int n)
    {
	int a = 0;
	int b = 1;
	// In N steps compute Fibonacci sequence iteratively.
	for (int i = 0; i < n; i++)
	{
	    int temp = a;
	    a = b;
	    b = temp + b;
	}
	return a;
    }

    static void Main()
    {
	for (int i = 0; i < 15; i++)
	{
	    Console.WriteLine(Fibonacci(i));
	}
    }
}

Output

0
1
1
2
3
5
8
13
21
34
55
89
144
233
377

If you just want to list Fibonacci numbers, you could change Fibonacci() to simply use Console.WriteLine(a). This would make the method print out each number as it is computed, which would reduce the total amount of computation required.

But: The time spent printing to the Console would dominate the program's real-world runtime.

Console.WriteLine

Overflow. One problem with this implementation is that the int types will overflow past the 47th Fibonacci number. To solve this problem, you can change Fibonacci() to return double. Also change a, b, and temp to be of type double.

IntDouble

Discussion. The Fibonacci sequence begins with zero. Fibonacci himself, in 1202, began it with 1, but modern scientists just use his name, not his version of the sequence. I tested the output of the program and it is correct.

Fibonacci began the sequence not with 0, 1, 1, 2, as modern mathematicians do but with 1, 1, 2.

Fibonacci: Wikipedia

Summary. Fibonacci numbers don't just reveal some interesting aspects of the world. They also teach us about computer programming. By storing the previous value in a loop iteration, we avoid a tremendous amount of computational work.

And: In the rare cases where Fibonacci numbers are critical, an iterative solution dramatically improves software.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf