+ Reply to Thread
Results 1 to 6 of 6

Thread: Fibonacci Sequence

  1. #1
    Code Warrior Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana's Avatar
    Join Date
    Oct 2008
    Posts
    4,058
    Blog Entries
    6

    Fibonacci Sequence

    I may be possibly moving away to Java soon (I've been playing with it quite a bit), so I may reproduced this tutorial in Java later on, if someone doesn't beat me.

    The Fibonacci Sequence is relatively simple - you take the initial number and the second number and add them together starting from 0 and 1, like so:
    Code:
    0 + 1 = 1
    1 + 1 = 2
    1 + 2 = 3
    2 + 3 = 5
    3 + 5 = 8
    and so on.
    This can be implemented in a simple way in C#.

    First, go to File, New Project, Console Application, Ok.
    Now, go in between the starting and closing brackets ( { and } ) of the Main function.
    Start off by declaring (and initializing) 3 integers, firstnumber, secondnumber and thirdnumber
    Code:
    int firstnumber = 0, secondnumber = 1, thirdnumber = 0;
    We have made secondnumber equal to 1, as our algorithim will plus the first number (which we have initialized to 0) and the secondnumber (initialized to 1) and then create our third number by adding them (we've initialized this to 0 for the sake of initializing them all)

    For now, we are going to hard code (yes I know, throw bricks at me everyone) the upper bound limit that we want to go to, however you could take the input from the user, store it in another integer and then check the third number against it. To do this, we will use a while loop. We will use 20 as our upper bound limit.

    Code:
    while (thirdnumber < 20)
    {
    Now, we have to add the first number and the second number and store it as the third number.

    Code:
    thirdnumber = firstnumber + secondnumber;

    Then, we must print out our thirdnumber to the console, while enforcing our upper limit, which we will do with an if statement.

    Code:
    if (thirdnumber < 20)
    {
    Console.WriteLine(thirdnumber);
    }
    Now, we must set the first and second numbers to their appropriate values, it is VERY important you do this in the right order and to the right variables. If you set the second number first, you won't be able to set the first number to the second number. So you must set the first number to the second number and then set the second number to the third number. Then we must finish off the while loop with a closing bracket. And then just for good measure, we'll add a read line after the while loop so that we don't get any annoying console closing problems.

    Code:
    firstnumber = secondnumber;
    secondnumber = thirdnumber;
    }
    Console.ReadLine();
    When you run this, it should show the numbers in the fibbonacci sequence upto your upper bound limit. The final code should look like this (including the bits that are automatically there when creating a new console project:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                int firstnumber = 0, secondnumber = 1, thirdnumber = 0;
                while (thirdnumber < 20)
                {
                    thirdnumber = firstnumber + secondnumber;
                    if (thirdnumber < 20)
                    {
                        Console.WriteLine(thirdnumber);
                    }
                    firstnumber = secondnumber;
                    secondnumber = thirdnumber;
                }
                Console.ReadLine();
            }
        }
    }
    If you liked my tutorial, please tip the scales (+rep) me
    My Site | Questions and Answers | Ask Me: Termana | Last Tutorial: Ajax innerHTML
    If you can keep your head while all around you are losing theirs, you probably have a CD writer on your desktop

  2. #2
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    37
    Posts
    12,912
    Blog Entries
    57

    Re: Fibonacci Sequence

    Usually, I've seen it as:
    1,1,2,3,5,8,13,21,...
    Why stop at 20?
    CodeCall Blog | CodeCall Wiki
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  3. #3
    Code Warrior Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana is a name known to all Termana's Avatar
    Join Date
    Oct 2008
    Posts
    4,058
    Blog Entries
    6

    Re: Fibonacci Sequence

    20 was really just a random number I came up with to use as an upper limit - the upper limit would be useful if you wanted to only see the numbers below that limit, so the program will display 1,1,2,3,5,8,13 but not 21 because that is above the limit. I put in a limit so the program didn't endlessly scroll off with numbers of the sequence.
    My Site | Questions and Answers | Ask Me: Termana | Last Tutorial: Ajax innerHTML
    If you can keep your head while all around you are losing theirs, you probably have a CD writer on your desktop

  4. #4
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,751
    Blog Entries
    97

    Re: Fibonacci Sequence

    This would be a good challenge because of the simplicity and complexity - "The most efficient version". +rep

  5. #5
    Programmer Mathematix is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    104

    Re: Fibonacci Sequence

    Fibonacci does start 0,1 and you only need two variables and not three for calculations and display.

    Nicely laid out, though!

  6. #6
    Programming Professional Parabola will become famous soon enough Parabola will become famous soon enough Parabola's Avatar
    Join Date
    Jul 2009
    Location
    Texas
    Age
    26
    Posts
    305
    Blog Entries
    3

    Re: Fibonacci Sequence

    Here is my "adaptation" of this code, which does include the user input:
    Code:
    using System;
    
    namespace Fibonacci
    {
         class FibonacciSequence
         {
              public static void Main()
              {
                   int firstNumber = 0;
                   int secondNumber = 1;
                   int thirdNumber = 0;
    
                   Console.Write("Please enter an upper limit: ");
                   string myInput = Console.ReadLine();
                   int myInt = Int32.Parse(myInput);
                   Console.Write("1");
    
                   while ((firstNumber + secondNumber) <= myInt)
                   {
                        thirdNumber = firstNumber + secondNumber;
                        Console.Write(", {0}", thirdNumber);
                        firstNumber = secondNumber;
                        secondNumber = thirdNumber;
                   }
                   Console.ReadLine();
              }
         }
    }
    The reason i used
    Code:
     ((firstNumber + secondNumber) <= myInt)
    was because if i just used third number, then it would always go over the upper limit.

    Also, a proper Fibonacci if I remember correctly goes 1,1,2,3..... so I had to add the extra 1 at the beginning.

    Either way, I'm a newbie to programming (seriously, this is the first time using Visual Studio or C#), so any feedback would be appreciated. Thanks for the tutorial though!

    +reps to you!

    P.S. - if you like the band Tool, check out Lateralus- that song is a Fibonacci sequence. Just listen to the number of syllables in each line, not to mention he keeps singing about spiraling out.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Functional C# – Fun With Folds
    By Kernel in forum News
    Replies: 0
    Last Post: 02-26-2009, 09:20 AM
  2. incredibly large integers (Fibonacci sequence)
    By suicidal pencil in forum Perl
    Replies: 1
    Last Post: 02-24-2009, 08:16 PM
  3. Fibonacci with no recursion for fun
    By HAL 9000 in forum C and C++
    Replies: 10
    Last Post: 06-26-2008, 08:54 AM
  4. Please help!!!!
    By funkyjohny in forum C and C++
    Replies: 2
    Last Post: 11-05-2007, 09:38 AM
  5. Question about chained sequence
    By The_Master in forum C and C++
    Replies: 1
    Last Post: 12-10-2006, 04:39 PM