2

Im currently trying to make a program that prints the first 50 terms of the fibonnachi sequence. The Fibonacci sequence goes like this. 0,1,1,2,3,5,8,13 The Nth term is the sum of the previous two terms. So in the example above the next term would be 21 because it would be theprevious two terms added together (8+13).

my code currently does not display this could someone help me to understand why?

static void Main(string[] args)
    {
        int[]   fibonnachi  = new int[50];
        fibonnachi[0] = 0;
        fibonnachi[1] = 1;
        int fib2ago = 0;
        int fib1ago = 1;
        for (int counter = 2; counter < 51; counter++)
        {
            fibonnachi[counter] = fibonnachi[fib2ago] + fibonnachi[fib1ago];
            Console.Write(fibonnachi[counter] + " ,");
            fib2ago++;
            fib1ago++;

        }
        Console.ReadLine();
    }

6 Answers6

3

Maybe you don't want a version that breaks at maxInt with an overflow, that mixes concerns by calculating and outputing in the same function or some that uses pre-dimensioned arrays to soil up the memory ;)

So here is a fun little version yielding an infinte sequence:

    public static IEnumerable<System.Numerics.BigInteger> Fibonacci()
    {
        System.Numerics.BigInteger current = 0, next = 1;
        while (true) 
        {
            yield return current;
            next = current + next;
            current = next - current; // isn't mutation ugly to read?
        }
    }

you can use it for example like this:

    foreach (var i in Fibonacci().Take(10)) 
    {
        Console.Write("{0} ,", i);
    }

    > 0 ,1 ,1 ,2 ,3 ,5 ,8 ,13 ,21 ,34 ,

Note you might have it reference Sytem.Numerics for the BigInteger and maybe you have to think about the trick with b1, b2 a bit - this is just because I did not want to introduce a dummy variable to remember b2 to update b1 ;)

Shameless ads:

Of course you can do this in a much more readable way using recursion:

public static IEnumerable<System.Numerics.BigInteger> 
    Fibonacci(System.Numerics.BigInteger current,
              System.Numerics.BigInteger next)
{
    yield return current;
    foreach(var n in Fibonacci(next, current+next))
        yield return n;
}

beeing C# this will maybe blow your memory after some time (I don't know how the compiler handles the recursive looping stuff here really) - but this is so much more natural in F# anyway:

let fibonacci =
    let rec create current next =
        seq {
            yield current
            yield! create next (current + next)
        }
    create 0I 1I

or maybe more idiomatic

let fibonacci =
    (0I, 1I)
    |> Seq.unfold (fun (current, next) -> 
        Some (current, (next, current + next)) )

And if you want to see really good stuff have a look at this:

The Fibonacci sequence - HaskellWiki :)

Random Dev
  • 51,810
  • 9
  • 92
  • 119
  • "isn't mutation ugly to read?" yes, how about `int previous = current; current = next; next = previous + current;` – weston Oct 11 '14 at 11:34
  • @weston yep - but this somewhat defeats the purpose here ;) (I kindof wanted to give something very similar using recursion next) – Random Dev Oct 11 '14 at 11:36
2

The problems in the code are:

  • You are looping to 50, but the highest index in the array is 49.
  • You are increasing counter twice in the loop.
  • The first two numbers are not displayed by the loop, so you have to do that first.

Updated code:

int[] fibonnachi = new int[50];
fibonnachi[0] = 0;
fibonnachi[1] = 1;
int fib2ago = 0;
int fib1ago = 1;
Console.Write(fibonnachi[0] + " ,");
Console.Write(fibonnachi[1] + " ,");
for (int counter = 2; counter < 50; counter++) {
  fibonnachi[counter] = fibonnachi[fib2ago] + fibonnachi[fib1ago];
  Console.Write(fibonnachi[counter] + " ,");
  fib2ago++;
  fib1ago++;
}

Cleaning up the code a bit, you can use just one counter, and incorporate the first two items in the loop by checking when to start calculating new values:

int[] fibonnachi = new int[50];
fibonnachi[0] = 0;
fibonnachi[1] = 1;
for (int counter = 0; counter < 50; counter++) {
  if (counter >= 2) {
    fibonnachi[counter] = fibonnachi[counter - 2] + fibonnachi[counter - 1];
  }
  Console.Write(fibonnachi[counter] + " ,");
}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
1

Overall better approach would be this one :

    for (int i = 0; i < 51; i++)
    {
        fibonnachi[i+2] = fibonnachi[i] + fibonnachi[i+1];
        Console.Write(fibonnachi[i] + " ,");
    }
libik
  • 22,239
  • 9
  • 44
  • 87
1

Here says it all

static void Main(string[] args)
{
    Console.WriteLine("Please enter a number");
    int number = Convert.ToInt32(Console.ReadLine());
    Fibonacci(0, 1, 1, number);
}   

public static void Fibonacci(int a, int b, int counter, int number)
{
    Console.WriteLine(a);
    if (counter < number) Fibonacci(b, a+b, counter+1, number);
}
Community
  • 1
  • 1
Ghasem
  • 14,455
  • 21
  • 138
  • 171
1

use below code :

Recussive :

      public static int CalculateFibonacci(int n)
       {
          if(n == 0 || n == 1)
           return n;
          else
          return ( CalculateFibonacci(n-1) + CalculateFibonacci(n-2) );
        }

Non Recursive :

int a = 0;
int b = 1;
int c = 1;

for (int i = 0; i < n; i++)
{
    c = b + a;
    a = b;
    b = c;
}
return c;
Kumar Manish
  • 3,746
  • 3
  • 37
  • 42
0

You need to print first two number manually before loop started.

                 Console.WriteLine(fibonnachi[0]);
                 Console.WriteLine(fibonnachi[1]);
Devesh
  • 4,500
  • 1
  • 17
  • 28