views:

956

answers:

5

hi , urm... i am practising c# console application , and am trying to get the function to verify if the number appears in a fibonacci series or not but im getting errors

what i did was

 class Program
{
    static void Main(string[] args)
    {
        System.Console.WriteLine(isFibonacci(20));
    }
    static int isFibonacci(int n)
    {
        int[] fib = new int[100];
        fib[0] = 1;
        fib[1] = 1;
        for (int i = 2; i <= 100; i++)
        {
            fib[i] = fib[i - 1] + fib[i - 2];

            if (n == fib[i])
            {
                return 1;
            }



        }
        return 0;
    }
}

can anybody tell me what am i doing wrong here.....

+3  A: 

Well, for starters your array is only 10 long and you're filling it with ~100 items (out-of-range-exception) - but there are better ways to do this...

for example, using this post:

long val = ...
bool isFib = Fibonacci().TakeWhile(x => x <= val).Last() == val;
Marc Gravell
Ah, you beat me!
Joseph
oh sorry guys my bad , it was a typing mistake but that is not the problem ...
jarus
+2  A: 

One thing you can do is check for an early exit. Since you're trying to determine if a given number is in the Fibonacci sequence, you can do bounds checking to exit early.

Example:

static bool isFibonacci(int n)
{
    int[] fib = new int[100];
    fib[0] = 1;
    fib[1] = 1;
    for (int i = 2; i <= fib.Length; i++)
    {
        fib[i] = fib[i - 1] + fib[i - 2];

        if (n == fib[i])
        {
            return true;
        }
        else if (n < fib[i])
        {
            return false;  //your number has been surpassed in the fib seq
        }
    }
    return false;
}
Joseph
Probably should throw an exception if the end of the array is met: function is not able to give an answer in this case.
Richard
+2  A: 
int[] fib = new int[10];
for (int i = 2; i <= *100*; i++)

You're going out of the bounds of your array because your loop conditional is too large. A more traditional approach would be to bound the loop by the size of the array:

for (int i = 2; i < fib.Length; i++)

And make your array bigger, but as Marc said, there are better ways to do this, and I would advise you spend some time reading the wikipedia article on Fibonacci numbers.

Patrick
+9  A: 

Here's a fun solution using an infinite iterator block:

IEnumerable<int> Fibonacci()
{
   int n1 = 0;
   int n2 = 1;

   yield return 1;
   while (true)
   {
      int n = n1 + n2;
      n1 = n2;
      n2 = n;
      yield return n;
   }
}

bool isFibonacci(int n)
{
    foreach (int f in Fibonacci())
    {
       if (f > n) return false;
       if (f == n) return true;
    }
}
Joel Coehoorn
+5  A: 

The problem lies in <= the following statement:

for (int i = 2; i <= 100; i++)

more to the point the =. There is no fib[100] (C# zero counts) so when you check on i=100 you get an exception.

the proper statement should be

for (int i = 2; i < 100; i++)

or even better

for (int i = 2; i < fib.Length; i++)
Justin