The Fibonacci series (sequence, numbers) is an infinite sequence of integers, in which the first number is 0, the second 1 and every next number is defined as a sum of two preceding numbers. Hence the sequence starts with numbers 0,\\;1,\\; 1,\\; 2,\\; 3,\\; 5,\\; 8,\\; 13,\\; 21,\\; 34\\cdots

The series was discovered by Italian mathematician of the 12th century Leonardo Pissano (so called Fibonacci) as a model for growth of a rabbit population.

  • At the beginning one pair of rabbits is born.
  • Only rabbits older than one month can reproduce.
  • Every productive pair of rabbits gives birth to one new pair of rabbits every month.
  • The rabbits never die, when they start to mate, they never cease to.

Fibonacci series in algorithmics

From the point of view of algorithmics is the Fibonacci series very interesting problem, because it shows how inefficient algorithm may be created by simply rewriting the rules into the code. In this case the inefficient implementation is recursion based.

    /**
     * Fibonacci sequence using recursion
     * @param index of the number (starting at 0)
     * @return nth Fibonacci number 
     */
    public static int fibonacciRec(int index) {
        if(index == 0) return 0;
        else if(index == 1) return 1;
        else return fibonacciRec(index - 1) + fibonacciRec(index - 2);
    }
/**
 * Fibonacci sequence
 * @param to order of a number (counting from 0)
 * @return Fibonacci number at the given position
 * @autor Thomas (www.adamjak.net)
 */
int fiborec(int to)
{
    if (to == 0)
    {
        return 0;
    }
    else if (to == 1)
    {
        return 1;
    }
    else
    {
        return fiborec(to - 2) + fiborec(to - 1);
    }
}
/**
 * Fibonacci sequence
 * @param to order of a number (counting from 0)
 * @return Fibonacci number at the given position
 * @autor Thomas (www.adamjak.net)
 */
public static int FibonacciRec(int to)
{
    if (to == 0)
    {
        return 0;
    }
    else if (to == 1)
    {
        return 1;
    }
    else
    {
        return FibonacciRec(to - 1) + FibonacciRec(to - 2);
    }
}
/** 
 * Fibonacci sequence
 * @param $to order of a number (counting from 0)
 * @return Fibonacci number at the given position
 * @autor Thomas (www.adamjak.net)
 */
function fibonacci_rec($to) {
    if ($to == 0) {
        return 0;
    }
    else if ($to == 1) {
        return 1;
    }
    else{
        return fibonacci_rec($to - 1) + fibonacci_rec($to - 2);
    }
}
Fibonacci series
Fibonacci series – recursion

Why is the recursive implementation inefficient?

The recursive implementation is by definition correct, but if we consider calculating the fifth Fibonacci number, than we have to calculate Fib(3) twice, Fib(2) three times and trivial values will be calculated 8 times. With increasing value of the parameter the inffeciency only grows.

Iterative implementation of Fibonacci series

The solution is simple – transforming the problem into its iterative form, which can be done in this case by using a very simple form of dynamic programming (dynamic programming is based on remembering and reusing of already computed values). This can be done by introducing two helper variables that will be used to store the previous two Fibonacci numbers.

    /**
     * Fibonacci sequence using dynamic programming
     * @param order of a number (counting from 0)
     * @return Fibonacci number at the given position
     */
    public static int fibonacci(int index) {
        if(index == 0) return 0;
        if(index == 1) return 1;
        int prePre = 0;
        int pre = 1; 
        int result = 0;
        for(int i = 1; i < index; i++) { //calculate starting the index 2
            result = prePre + pre; //result is sum of two preceding numbers
            prePre = pre; 
            pre = result; 
        }
        return result;
    }
/**
 * Fibonacci sequence
 * @param to order of a number (counting from 0)
 * @return Fibonacci number at the given position
 * @autor Thomas (www.adamjak.net)
 */
static int Fib(int to)
{
    if (to == 0)
    {
        return 0;
    }
    if (to == 1)
    {
        return 1;
    }

    int last_last = 0, last = 1, result = 0, i;

    for (i = 1; i < to; i++)
    {
        result = last_last + last;
        last_last = last;
        last = result;
    }

    return result;
}

/**
 * Fibonacci sequence
 * @param to order of a number (counting from 0)
 * @return Fibonacci number at the given position
 * @autor Thomas (www.adamjak.net)
 */
static int Fibonacci(int to)
{
    if (to == 0)
    {
        return 0;
    }
    if (to == 1)
    {
        return 1;
    }
    int last_last = 0;
    int last = 1;
    int result = 0;
    for (int i = 1; i < to; i++)
    {
        result = last_last + last;
        last_last = last;
        last = result;
    }
    return result;
}
/**
 * Fibonacci sequence
 * @param $to order of a number (counting from 0)
 * @return Fibonacci number at the given position
 * @autor Thomas (www.adamjak.net)
 */
function fibonacciho_postupnost($to) {
    if ($to == 0) {
        return 0;
    }
    if ($to == 1) {
        return 1;
    }
    
    $last_last = 0; 
    $last = 1; 
    $result = 0; 
    for($i = 1; $i < $to; $i++){ 
        $result = $last_last + $last; 
        $last_last = $last; 
        $last = $result; 
    }
    return $result;
    
}

SEO od společnosti Digital Pylon


Online casino s algoritmem

České casino online online slot-vegas.cz

Hrajte nejlepší hry jako je GoodGame Empire.





Zajímavé články: Jak najít práci snů? Zvolte kariéru v IT!, Češi mají rádi hrací automaty online, Jak funguje algoritmické obchodování Casino, Online výuka Algoritmus a online marketing mají svá pravidla, Automaty, Matematický vliv, Ratings, Jak fungují algoritmy hazardních her online: více znalostí, více peněz, SYPWAI - nástroj pro vědecký vývoj, Vynikají na globálním trhu: Nejlepší vývojáři softwaru pro online výherní automaty, Jak si vybrat nejlepší české online casino, Proč byste měli hrát online casino VPN revoluce, Kde najdeme algoritmy v každodenním životě?, Čeká vás pracovní pohovor mimo město? Podívejte se, jak dokonale zvládnout včasný příchod, 5 úžasných technologií ze světa hazardních her, Mirror and access to Mostbet, Svou kancelář můžete mít stále po ruce, Jaké výhody má digitalizovaná firma oproti off-line konkurenci?, Jaký systém vybrat pro snadné řízení výroby?, Nahradí umělá inteligence ajťáky?, Důvody, proč používat SnapTik ke stahování videí TikTok, Dokonalý den na pláži: Co si vzít s sebou, aby byl výlet zábavný a bezpečný?, Jak přežít dlouhý let?, Go pay GoodGame Empire, Blockchain, Rozhovor, Umělá inteligence, Ochranná známka pre softvér: Prečo ju registrovať?, Role kryptografických algoritmů v zabezpečení online kasin, Jaké jsou náklady na nákup 3D tiskárny?, Jak algoritmy vylepšují online zážitky v roce 2025, Epilace laserem a péče o pokožku před a po ní, Byty k pronájmu Sokolov - výhody a rizika pronájmu bytu bez realitky, Filmy a seriály plné hádanek: kryptografie jako hlavní téma Kdy obnovit data z disku běžně dostupným softwarem a kdy už se obrátit na profesionály?>


Doporučujeme

Internet pro vaši firmu na míru

https://www.algoritmy.net