0% found this document useful (0 votes)
30 views3 pages

Fibonacci

Uploaded by

Piron Live
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views3 pages

Fibonacci

Uploaded by

Piron Live
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Fibonacci numbers

The Fibonacci sequence is named after Italian mathematician Leonardo of Pisa,


known as Fibonacci:
https://en.wikipedia.org/wiki/Fibonacci_number

The Fibonacci numbers fn = f(n) are the numbers characterized by the fact that
every number after the first two is the sum of the two preceding ones. They are defined
with the next recurrent relation:
0, if n = 0

f (n) = 1, if n = 1
 f (n − 1) + f (n − 2)

So f0 = 0, f1 = 1, fn = fn-1 + fn-2.
The Fibonacci sequence has the form
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, …

Example. Fill integer array fib with Fibonacci numbers (fib[i] = fi):
#include <stdio.h>

int i, n, fib[47];

int main(void)
{
scanf("%d",&n);

fib[0] = 0; fib[1] = 1;
for(i = 2; i <= n; i++)
fib[i] = fib[i-1] + fib[i-2];

printf("%d\n",fib[n]);
return 0;
}

i 0 1 2 3 4 5 6 7 8 9 10 ...

fib[i] 0 1 1 2 3 5 8 13 21 34 55 ...

The biggest Fibonacci number that fits into int type is


f46 = 1836311903
The biggest Fibonacci number that fits into long long type is
f92 = 7540113804746346429
If you want to find Fibonacci number fn for n > 92, use BigInteger type.
Example. Find f(n) – the n-th Fibonacci number with recursion:
#include <stdio.h>

int n;

int fib(int n)
{
if (n == 0) return 0;
if (n == 1) return 1;
return fib(n-1) + fib(n - 2);
}

int main(void)
{
scanf("%d",&n);
printf("%d\n",fib(n));
return 0;
}

f(5)
f(4) + f(3)

f(3) + f(2) f(2) + f(1)

f(2) + f(1) f(1) + f(0) f(1) + f(0)

f(1) + f(0)

Example. Find f(n) – the n-th Fibonacci number with recursion + memoization:
#include <stdio.h>
#include <string.h>

int n, fib[46];

int f(int n)
{
// base case
if (n == 0) return 0;
if (n == 1) return 1;

// if the value fib[n] is ALREADY found, just return it


if (fib[n] != -1) return fib[n];

// if the value fib[n] is not found, calculate and memoize it


return fib[n] = f(n-1) + f(n - 2);
}
int main(void)
{
scanf("%d",&n);

// fib[i] = -1 means that this value is not calculated yet


memset(fib,-1,sizeof(fib));

printf("%d\n",f(n));
return 0;
}

f(5)
f(4) + f(3)
mem
f(3) + f(2)
mem
f(2) + f(1)

f(1) + f(0)

E-OLYMP 4730. Fibonacci Fibonacci numbers is a sequence of numbers F(n),


given by the formula:
F(0) = 1, F(1) = 1, F(n) = F(n – 1) + F(n – 2)
Given value of n (n ≤ 45). Find the n-th Fibonacci number.
► Implement a recursive function with memoization.

E-OLYMP 8295. Fibonacci string generation Generate the n-th Fibonacci string
that is defined with the next recurrent formula:
• f(0) = "a";
• f(1) = "b";
• f(n) = f(n – 1) + f(n – 2), where "+" operation means concatenation
For example, f(3) = f(2) + f(1) = (f(1) + f(0)) + f(1) = "b" + "a" + "b" = "bab".
► Implement a recursive function that generates the n-th Fibonacci string.

string f(int n)
{
if (n == 0) return "a";
if (n == 1) return "b";
return f(n-1) + f(n-2);
}

Read input value of n and print the n-th Fibonacci string.


cin >> n;
cout << f(n) << endl;

You might also like