Fibonacci Generator Using Python

Python Fibonacci Generator

The Fibonacci sequence is a mathematical formula that arranges elements in an interesting arrangement, as each element in the sequence is the sum of the previous two elements. This numerical phenomenon is utilized in various computer languages such as C, C++, JAVA, and Python, but regardless of the language, the logic behind its implementation is the same. To explore the logic of this sequence and see how it is implemented in Python, let’s examine the Fibonacci series.

What is the Fibonacci Sequence?

Starting from zero, the Fibonacci sequence consists of consecutive, additive terms that culminate in an infinite series: 0,1,1,2,3,5,8… Therefore, in order to generate the Fibonacci series, the sum of two numbers must first be determined; this sum then serves as the basis for the following number in the series.

def fibonacci_sequence():
    a=0
    b=1
    for i in range(7):
        print(b)
        a,b= b,a+b

obj = fibonacci_sequence()

To begin, the initialization of the Fibonacci generator is declared, and the starting calculation is given a value of 1. From here, the sum of two numbers is printed in sequence, completing the Fibonacci series. The for loop is used up to 7 numbers of sequences. Then, the object is created to print the sequence as a result.

Simple Fibonacci Sequence
Simple Fibonacci Sequence

How to Generate One Value At A Time From the Sequence?

To generate one value at a time in the Fibonacci sequence, we are using the next() function of Python. The next () function always returns the next value/output from the sequence. This method is very useful when we need only a few values from the Fibonacci sequence. This prints only those values for which the next() function is implemented. The object is created to print the elements from the sequence. Let’s try out this method.

def fibonacci_Generator():
    a=0
    b=1
    for i in range(6):
        yield b
        a,b= b,a+b

obj = fibonacci_Generator()
 
print(next(obj))
print(next(obj))
print(next(obj))
print(next(obj))

In this code, the yield method is used, which works like a return statement for objects. Here, only the first four values are printed using next() function. Let’s see the implementation of this code.

Fibonacci Sequence
Fibonacci Sequence

You can see in the output only the first four values are printed as an output.

Fibonacci Generator Using Python For Infinite Values

The Fibonacci generator will be able to print the value for infinite times. For this method, we are using a while loop instead of a normal for loop. The value of the while loop will be set to TRUE so that the loop will not exit in the future. Whenever we call the object, it will print the value, and the sequence will continue. This way, we can develop a Fibonacci generator for infinite values. Let’s try the code.

def fibonacci_Generator():
    a=0
    b=1
    while(True):
        yield b
        a,b= b,a+b

obj = fibonacci_Generator()
 
print(next(obj))
print(next(obj))
print(next(obj))
print(next(obj))
print(next(obj))
print(next(obj))
print(next(obj))
print(next(obj))
print(next(obj))

In this code, the functions like next() and yield are used to print the next values. The only difference is the use of a while loop instead of for loop. Let’s see the output.

Fibonacci Generator
Fibonacci Generator

Whenever we call the object, the next value will print in the sequence. In this way, we can print infinite values of this sequence, as you can see in the results.

Use Of Fibonacci Sequence In Various Domain

Machine Learning and Deep Learning Models

The Fibonacci sequence is widely used in the machine learning and deep learning domain. This sequence is based on logic and mathematical formula, so the logic helps in pattern searching and optimization techniques. Based on this sequence and patterns, different problems are solved. The model, which is purely based on ratios, arithmetic equations, and fractions, uses this sequence as a reference point.

Algorithm Techniques

In different algorithms like dynamic programming, optimization, Fibonacci searching, and sorting, we can use the Fibonacci sequence. These algorithms are very complex, but this Fibonacci sequence is considered a base for understanding these problems. Many algorithms in the searching and sorting domain are implemented using the Fibonacci sequence.

Biological Models

The different models for the detection of biological patterns of stems, plants, and shells are based on the numerical sequence of Fibonacci. So, understanding this sequence is very important to predict and classify these things using biological patterns.

Market Analysis and Trading Patterns

The Fibonacci sequence is used to develop the code/ models which will predict the trading patterns and market analysis. The main use of the Fibonacci sequence is pattern searching, so this is very useful for predicting the results.

Design and Art concepts

In the design and art concepts, we can use the Fibonacci sequence to take ratios of different elements in the art. These patterns from the Fibonacci sequence make art pieces more aesthetic and precise. The use of this technique in the art makes it more attractive visually and precise by measurements.

Summary

In this article, we have seen different techniques and methods to implement the Fibonacci sequence for a finite and infinite number of elements. This article also gives details about the use of the Fibonacci sequence in different domains. Hope you will enjoy this article.

References

Read Stack Overflow for more reference.