Recursive fibonacci printing

3

I have problems with the recursive fibonacci function, in the exercise you are asked to print inside the function or even using an auxiliary recursive function, but it can not print in the main function, I tried everything for printing, however they keep repeating themselves, the recursive fibonacci function I know how to do, it just lacks the impression, it follows the enunciando and I ask someone to give me a light.

  

"The Fibonacci Sequence is defined by:

     
  • F0 = 0
  •   
  • F1 = 1
  •   
  • Fn = F n - 1 + F n - 2 , for n> 1
  •   

Develop a recursive function that calculates and prints the first n   numbers of the Fibonacci Sequence. The function should have the following signature:

int fibonacci (int n);
     

The function should print the first n numbers separated by spaces, for   For example, for n = 10:

     

0 1 1 2 3 5 8 13 21 34 "

    
asked by anonymous 16.01.2017 / 19:10

3 answers

1

Remember what the function is about is the sum of the predecessor by the previous predecessor, it begins with two terms 0 and 1.

    (1 + 0), (1 + 0) +1), ((1 + 0) +1) + (1 + 0)) ....
we can arrive at Fibro (n - 1) + Fibro (n - 2) where n is the number of terms considering, of course, that it is being decremented (so that it needs to pass only a parameter value).

made recursively each sum of its terms will be stored in a stack which after reaching the base condition will be closed and will have its terms unpinned.

obs: I leave an example in C #, the reasoning is the same

  static void Main(string[] args)
    {
        int n=0;
        Console.WriteLine("Digite o tamanho da sequencia");
        n = int.Parse(Console.ReadLine());

        Console.WriteLine("resultado");
        for (int c=0; c<=n; c++) {
          Console.Write(" "+Fibro(c + 1));//imprimi aqui mas você pode imprimir dentro da recursão mas vai perder o objetivo da recursão de mostrar uma pilha 
        }
        Console.ReadKey();

    }

    public static int Fibro(int n){
        if (n == 1 || n == 0)
        {
            return 1;
        }
        else {

            return Fibro(n - 1) + Fibro(n - 2);//recursão 

        }

     }
    
04.03.2018 / 16:36
0

If you only need to print, each time you calculate the number value,

printf("Fibonacci é: %d", fibonacci);

Where% d is just a holder for your number, which is passed in the second parameter.

    
16.01.2017 / 19:16
0

This is a version of the fibonacci algorithm using an auxiliary vector, which makes it possible to calculate values up to 50, instantly. For larger values, you will need the use of long int, long long int or others.

#include <stdio.h> 
#include <stdlib.h> 

void calculaFibonnaci();
int fibonnaci();

int vetor[1000];

void calculaFibonnaci(int i) { 

    if(i==1 || i==2) {
        vetor[i] = 1;
    }
    else { 
        vetor[i] = fibonnaci(i-1) + fibonnaci(i-2);
    }
}

int fibonnaci(int i)
{
    if(vetor[i]==0) {
        calculaFibonnaci(i);
        printf("%d ", vetor[i]);
    }
    else {
        return vetor[i];
    }   
} 

int main(int argc, char* argv[]) 
{
    int i, N;
    int *vetor;
    printf("Digite quantos termos da série de fibonacci você deseja: ");
    scanf("%d", &N);

    for(i=1; i<=N; i++)
        fibonnaci(i);

    return 0;
} 
    
16.01.2017 / 19:21