Fibonacci series in python

 Fibonacci sequence


The sequence of integer numbers known as the Fibonacci sequence is pretty well-known. The sequence has a nice recursive definition and appears naturally in many problems.


The pragmatic programmer's journey toward mastering recursion begins with mastering its generation. You will learn about the Fibonacci sequence and how to make it with Python in this tutorial.


The Fibonacci Sequence's Recursion in the Background


A well-known recursive problem is creating the Fibonacci sequence. Recursion is when a function breaks down the problem it is trying to solve by referring to itself.


The problem gets smaller with each function call until it reaches a base case, at which point it returns the result to each intermediate caller before returning the final result to the first caller.


Recurring solutions to the same problem are depicted by the colored subproblems. Because of this, in order to recursively generate a Fibonacci sequence, numerous intermediate numbers must be repeatedly calculated.


The recursive approach to the Fibonacci sequence has a number of fundamental problems, one of which is this.


Fibonacci Spiral

When we make squares with these widths, we get a spiral, which is an interesting property of these numbers. A Fibonacci spiral is a pattern of quarter-circles that are connected within a square block with Fibonacci numbers written in each block.


The sum of the two smaller squares that come after the giant square is the number. With each block having a higher number than the two blocks before it, this is an ideal arrangement. The Logarithmic pattern, which also appears similar, served as the basis for the main idea.


The golden ratio is also connected to these numbers.


Methods Through which Fibonacci Series can be Generated


  1. Through Generators


Because the function xrange is a generator of numbers between 0 and num and yield is a generator of formatted output, this approach is referred to as a "generator."


  1. Through for loop


The Fibonacci series from 0 to 10 has been printed with a straightforward for loop. The variables have been given new values inside the for loop.


The default initial values of Fibonacci have been set to 0 and 1, respectively, for the variables U and v.


The new u value is the old v value as the loop continues to run, whereas the new v value is the sum of the old u and v values until the range's end.


  1. Through Recursion

To print the Fibonacci series, the function "fibonacci_ser" is calling itself.

As a result, the method is known as "recursion."


Here are the steps taken:


The user has been asked to enter the location where the Fibonacci series should be printed up until this point.


The "fibonacci_ser" function processes numbers.


If the length provided is less than 1, the condition is checked. If that's the case, the result is given right away.


Recursive calls to "fibonacci_ser" with arguments of lengths less than 1 and 2 are made if the length is greater than 1, i.e. fibonacci_ser(m-1) and fibonacci_ser(m-2).

As a result, recursion prints the desired output.


So, in a nutshell, we talked about three ways to show the Fibonacci series.

through generators, the for loop, recursion, and generators.


Conclusion


If one had read the Fibonacci material above, one would have a solid understanding of the Python-specialized Fibonacci numbers and series.


Once you get used to the logic of the Fibonacci series, it will be easy to create other series, work with other numbers, and use a variety of methods. Only by taking a logical approach can you succeed in this.


           I hope that my article was beneficial to you. To learn more, click the link here

Comments

Popular posts from this blog

Web activities in azure data factory

DevOps engineer skill

DevOps deployment tools