Recursive and non-recursive implementation of Fibonacci sequence

Source: Internet
Author: User

#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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.