C# fibonacci function returning errors

c#, console-application, fibonacci

Solution

And here is a solution that beats all of yours!

Because, why iteration when you have smart mathematicians doing closed-form solutions for you? :)

static bool IsFibonacci(int number)
{
    //Uses a closed form solution for the fibonacci number calculation.
    //http://en.wikipedia.org/wiki/Fibonacci_number#Closed-form_expression

    double fi = (1 + Math.Sqrt(5)) / 2.0; //Golden ratio
    int n = (int) Math.Floor(Math.Log(number * Math.Sqrt(5) + 0.5, fi)); //Find's the index (n) of the given number in the fibonacci sequence

    int actualFibonacciNumber = (int)Math.Floor(Math.Pow(fi, n) / Math.Sqrt(5) + 0.5); //Finds the actual number corresponding to given index (n)

    return actualFibonacciNumber == number;
}

Problem

I am practising a C# console application, and I am trying to get the function to verify if the number appears in a fibonacci series or not but I'm 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?

Original source

Related problems