Create Fibonacci in a linear function with delegate

5

Using a list for Delegate (I learned that this is called a linear function, is that right?) I decrement the list so it gets its values, but I can not do a simple arithmetic operation with "X", because I have a list and "X" happens to be this list, as Omni and ramaral explained me in the posts below.

Fibonacci I know how to do, I want to do it in the way mentioned.

I know Fibonacci is: Fn = F(n-1) + F(n-2) . x - 1 and x - 2 give error.
In this case the function return would be a list (Fibonacci)

See how my method got.

List<int> lista = new List<int>();

Func<List<int>, List<int>> calcFib = null;

for(int i = 0; i <= tamanho-1; i++)
    lista.Add(i);

calcFib = x => x.Count == 0 ? 0 : (x - 1 + x - 2) + calcFib(x.Take(x.Count - 1).ToList<int>());

return calcFib(lista);

My list, is the limit of the sequence I want to implement Fibonacci. In this case, the var tamanho . If tamanho for 10 , for example, would be the result: 0,1,1,2,3,5,8,13,21,34

public List<int> novoFibonacci(int tamanho)
        {
            List<int> lista = new List<int>();

            Func<List<int>, List<int>> calcFib = null;

            for(int i = 0; i <= tamanho-1; i++)
                lista.Add(i);

            calcFib = x => x

            return calcFib(lista);
        }
    
asked by anonymous 09.07.2015 / 14:23

2 answers

5

You should pick up each of the values from your list and calculate the corresponding value in the Fibonacci series

It would look something like this:

public List<int> novoFibonacci(int tamanho)
{
    List<int> lista = new List<int>();
    for (int i = 0; i <= tamanho - 1; i++)
    {
        lista.Add(i);
    }

    //O Select executa GetFibonacciAt() passando a ela cada um dos elementos da lista, 
    //returnado um IEnumerable<int> contendo cada um dos resultados.

    return lista.Select(n => GetFibonacciAt(n)).ToList();
}

You must create the int GetFibonacciAt(int n) method that calculates the value corresponding to n in the Fibonacci series.

If I want to post this function.

EDIT

Solution with an inline function as request:

public List<int> novoFibonacci(int tamanho)
{
    Func<int, int> getFibonacciAt = 
               n => (n <= 1) ? n : getFibonacciAt(n - 1) + getFibonacciAt(n - 2);

    var lista = new List<int>();
    for (int i = 0; i <= tamanho - 1; i++)
    {
        lista.Add(i);
    }
    return lista.Select(n => getFibonacciAt(n)).ToList();
}
    
09.07.2015 / 16:09
4

You can use two online functions, or just one more complex one, to calculate Fibonacci.

    //função 1 recursividade (usando duas funções em linha)
    Func<int, int> calcFib = null;
    calcFib = 
        x => 
        (x < 2) ? x : calcFib(x-1) + calcFib(x-2);
    Func<int, List<int>> Fibonacci =
        x => 
        Enumerable.Range(1, x).Select(s => calcFib(s)).ToList();


    //função 2 somente linq (usando apenas uma função em linha)
    Func<int, List<int>> Fibonacci2 =
        x => 
        Enumerable.Range(1, x).Select(
            a => 
            Enumerable.Range(1, a).Skip(2).Aggregate(
                new KeyValuePair<int, int>(1, 1), 
                (seq, index) => 
                new KeyValuePair<int, int>(seq.Value, seq.Key + seq.Value)
            ).Value
        ).ToList();

Example on DotNetFiddle

    
10.07.2015 / 06:58