+ Reply to Thread
Results 1 to 6 of 6

Thread: Fibonacci Sequence

  1. #1
    Join Date
    Oct 2008
    Posts
    4,060
    Blog Entries
    6
    Rep Power
    45

    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
    Interested in participating in community events?
    Want to harness your programming skill and turn it into absolute prowess?
    Come join our programming events!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,466
    Blog Entries
    74
    Rep Power
    143

    Re: Fibonacci Sequence

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

  4. #3
    Join Date
    Oct 2008
    Posts
    4,060
    Blog Entries
    6
    Rep Power
    45

    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.
    Interested in participating in community events?
    Want to harness your programming skill and turn it into absolute prowess?
    Come join our programming events!

  5. #4
    Jordan Guest

    Re: Fibonacci Sequence

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

  6. #5
    Mathematix is offline Programmer
    Join Date
    Jun 2009
    Posts
    112
    Rep Power
    0

    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!

  7. #6
    Parabola's Avatar
    Parabola is offline Programming Professional
    Join Date
    Jul 2009
    Location
    Texas
    Posts
    336
    Blog Entries
    4
    Rep Power
    13

    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. nth Fibonacci number using recursion
    By jackson6612 in forum C and C++
    Replies: 1
    Last Post: 05-21-2011, 03:31 PM
  2. Fibonacci difficult question
    By skd in forum C and C++
    Replies: 1
    Last Post: 04-24-2011, 12:34 AM
  3. Fibonacci with no recursion for fun
    By HAL 9000 in forum C and C++
    Replies: 14
    Last Post: 11-28-2010, 06:53 AM
  4. Beginner problem: Fibonacci sequence
    By TheGame in forum Python
    Replies: 4
    Last Post: 09-23-2010, 04:21 PM
  5. incredibly large integers (Fibonacci sequence)
    By suicidal pencil in forum Perl
    Replies: 1
    Last Post: 02-24-2009, 08:16 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts