Recursive fibonacci printing

I'm having problems with the recursive fibonacci function, in the exercise is asked to print inside the function or even using an auxiliary recursive function, but can not print in the main function, I tried everything to print, but they are repeating, the recursive fibonacci function I know how to do, just missing the print, follows the enunciating and I ask someone to give me a light.

" the Fibonacci Sequence is defined by:

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

Develop a recursive function that calculates and prints the first n Fibonacci sequence numbers. The function must have the following signature:

int fibonacci (int n);

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

0 1 1 2 3 5 8 13 21 34"

Author: Victor Stafusa, 2017-01-16

4 answers

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

  • Formula : 0, 1,(1+0),((1+0)+1),(((1+0)+1)+(1+0))....

By transforming this expression into a function we can get to Fibro (n - 1) + Fibro (n - 2) where n is the number of terms considering , Of course , that it is being done in a decreasing way (so that you need to pass only one parameter value).

Done recursively each sum of your terms will be stored in a stack that after reaching the base condition will be terminated and your terms will have been emptied.

Note: 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 

        }

     }
 1
Author: Alessandra Faria, 2018-03-04 22:31:16

If only the printing is missing, each time you calculate the value of the number just

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

Where %d is just a" holder " pro its number, which is passed in the second parameter.

 0
Author: leofontes, 2017-01-16 18:16:26

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 to use 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;
} 
 0
Author: Gabriel Pellegrino, 2017-01-16 18:46:16

Fibonacci Series in C:

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

unsigned long fibonacci();
unsigned long v[sizeof(int)] ;

unsigned long fibonacci(int n) {
   if (n <= 0)
      return -1;
   if((n <= 2)  && v[n] == 0) {
      v[n] = 1;
      printf("%lu ", v[n]);
   }
   else if (v[n] == 0) {
      v[n] = fibonacci(n-2) + fibonacci(n-1);
      printf("%lu ", v[n]);
   }

   return v[n];
}

int main(int argc, char* argv[]) {
   int in;
   printf("Entre com o tamanho da série: ");
   scanf("%d", &in);
   fibonacci(in);
   return 0;
}

Fibonacci Series in Python:

d = {}
def fib(n):
  if (n < 2):
    d[n] = n
    return d[n]
  if (d.keys().__contains__(n)):
    return d[n]    
  d[n] = fib(n - 2) + fib(n - 1)    
  return d[n]  

for i in range(0,101):
  print('fib(',i,'): ', fib(i))
 -3
Author: Antônio Sérgio Ferraz, 2019-09-07 14:17:12