Fibonacci Sequence in Python

 639 total views,  3 views today

In 1202, an Italian mathematician named Leonardo of Pisa, also known as Fibonacci, introduced repetitive numbers to Western Europe in Liber Abaci. His sequence in which each number is the sum of the previous pair. The sequence usually starts with zero and proceeds until the nth digit.

My example of the famed sequence of numbers is written in two different methods. The first is a simple function, and the other is the same but with recursion. The recursion method, I will go through in another article. As always, here is the code and the breakdown of the methodology.

The first method that I will illustrate it using a function. The function called find_figonacci takes only the number of iterations or cycles that you want to solve the sequence.

def find_fibonacci(iterations):

    The first task is to prepare the variables to be used. There are two variables, N1 & N2, to be used as the additive numbers for the sequence. They represent the latest number and the last number of the sequence. To start, they have to be initialized to zero and one.

    n1 = 0
    n2 = 1

    The variable count is a simple counter, and this variable counts the loops until the number reaches the requested number of iterations.

    count = 0

The variable the_result is a list where the sequence is kept until all calculations are completed. I use a list here to present the list in a proper series instead of a typical vertical list of numbers.

    the_result = []

This loop is where the calculations are done. It is done X times governed up to when the count is larger than the requested iterations.

    while count < iterations:

Now, I will append to the list the value of n1. I converted the value of n1 to a string printed with commas later after the function had been completed.

        the_result.append(str(n1))

This function prints out the sequence of numbers that illustrates the Fibonacci sequence. The variable nth contains the sums of the series. If this is what you want, a separate list could be added to track this value as the function is executed.

        nth = n1 + n2

We update the variables n1 and n2 to the last two numbers in the calculated sequence up to this point during the loop.

        n1 = n2
        n2 = nth

Now, the last step is to update the counter for this loop.

        count += 1

The loop has been completed, and now is the time to return the result of the sequence to the calling statements.

    return the_result

The last two statements take the input from the user for how many cycles we should run. Then the cycle count is used in the calling of the find_fibonacci function. The print statement will print out the list in a comma-separated pattern when the function has been completed.

n_terms = int(input("How many iterations do you want to solve: "))
print(", " .join(find_fibonacci(n_terms)))

The Source Code:

def find_fibonacci(iterations):
    n1 = 0
    n2 = 1
    count = 0
    the_result = []

    while count < iterations:
        the_result.append(str(n1))
        nth = n1 + n2
        n1 = n2
        n2 = nth
        count += 1

    return the_result


n_terms = int(input("How many iterations do you want to solve: "))
print(", ".join(find_fibonacci(n_terms)))

How many iterations do you want to solve: 20
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181

Process finished with exit code 0

Add Comment

Your email address will not be published. Required fields are marked *