Create Fibonacci in a linear function with delegate

Asked

Viewed 209 times

5

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

Fibonacci I know how to do, I want to do it.

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

Look how my method turned out.

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);
        }
  • What is this lista?

  • Sorry, it’s just that I copied from my previous post. I will edit and relocate, so I explain better.

2 answers

5


You should take each of the values in your list and calculate the corresponding value in the Fibonacci series

It would be something like that:

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 have to create the method int GetFibonacciAt(int n) calculating the value corresponding to n in the Fibonacci series.

If you want me to put that function.

EDIT

Solution with an online function as requested:

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();
}
  • 1

    Let me try it first. If I break my head and I couldn’t, then I ask for your help. Thanks in advance.

  • Apparently, it will be a method that returns a Fib, like: Fn = (Fn-1 + Fn-2), exactly what I’ve been doing around, I think that’s it, right?

  • making a linear method similar to this one doesn’t work, right? qdr = square => square.Count == 0 ? 0 : square.Last() * square.Last() + qdr(square.Take(square.Count - 1).Tolist<int>()); This one I sum the squares of the elements in an array. That’s what I’d like to do, only with Fibonacci.

  • I believe that in a linear way it does not give because the next value is calculated according to the previous one.

  • 1

    I get it, that’s exactly what I was looking for, understanding. This is all a study I’m doing to have greater mastery over certain subjects. A programmer’s everyday life is lived upon decisions based on certainties, on standards already tested. That’s what I’m doing. Thank you.

  • I’ve been thinking it over and found a way to do it online.

Show 1 more comment

4

You can use two online functions, or only 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 in Dotnetfiddle

Browser other questions tagged

You are not signed in. Login or sign up in order to post.