#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int a = 1;
int b = 1;
int c = 0;
int n = 0;
int i = 0;
printf ("Please enter the number of Fibonacci numbers you want to compute \ n");
scanf ("%d", &n);
printf ("%3d", a);
printf ("%3d", b);
for (i=2 i < n; i++)
{
c = a + B;
printf ("%3d", c);
A = b;
b = C;
}
System ("pause");
return 0;
}
This is a non recursive algorithm for Fibonacci sequences. Next, we will introduce the recursive algorithm of Fibonacci sequence, and then give us an analysis of the space complexity and time complexity of the two methods after the completion of the two.
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
void Fib (int a, int b, int n)
{
if (n <= 0)
Return
Else
n--;
int c = 0;
c = a + B;
A = b;
b = C;
printf ("%3d", c);
Fib (A, B, N);
}
int main ()
{
int a = 1;
int b = 1;
int c = 0;
int n = 0;
printf ("Please enter the number of Fibonacci numbers you want to display \ n");
scanf ("%d", &n);
printf ("%3d", a);
printf ("%3d", b);
n = n-2;
Fib (A, B, N);
System ("pause");
return 0;
}
This is a recursive algorithm for Fibonacci.