Like any user who is not linked to computing in the strictest sense of the word; but, with an immense interest in everything related to Science and Technology —especially in computing— I am always looking for something to learn in my spare time.
In most cases, it has to do with this world of Free Technologies and lately in particular with programming languages. The first stumbling block was to choose a certain language to start with, and when you are not sure why? or what for? you are going to study it, the choice may not be so simple. Obviously, in my case it was not that difficult after all, after a while of analysis I chose Python.
In the beginning, I had started studying C by watching a series of video tutorials focused on the C Premier Plus book taught by Richard Couture on LinuxCabal — they say that if you master C, the Python language is easier to learn, I don't think that's so much the case. At the time, I gave up on C to focus only on Python.
Likewise, no matter how complex a particular language maybe, if you are motivated to study it the results can be very encouraging. I particularly switched to Python due to the following reasons:
- It has become a very, very popular language, with a huge community.
- Extensive documentation for beginners and advanced users in both English and Spanish — very important for someone who has not studied computer science.
- Extensible, powerful and highly readable code.
- Other reasons that have to do with learning Django and other Python Frameworks dedicated to web development.
- Python's learning curve is shorter.
- With C at some point, I was going to crash.
I am happy with Python. What's Next?
At this point I am already somewhat familiar with the language, although I have studied very little, now I am looking for excuses to write a few lines of code and it is here that another obsession that I have always had is reborn: «The Fibonacci sequence».
This is nothing new, there are countless articles on it - even in Python books - I'm not inventing anything here. I just want to share with the community from my little experience how to obtain the numbers of the Fibonacci sequence using Python. But for this, we must first know a little about Fibonacci.
According to Wikipedia:
“In arithmetic, the Fibonacci arrangement (now and then erroneously called the Fibonacci arrangement) is the accompanying boundless succession of characteristic numbers: 0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597 The arrangement starts with the numbers 0 and 1, and from these, "each term is the amount of the past two", is the repeat connection that characterizes it.
The components of this grouping are called Fibonacci numbers. This progression was depicted in Europe by Leonardo de Pisa, a thirteenth-century Italian mathematician otherwise called Fibonacci.
It has various applications in software engineering, arithmetic, and game theory. It additionally shows up in natural designs, for example, in the parts of trees, in the plan of leaves on the stem, in the blossoms of artichokes and sunflowers, in the inflorescences of Romanesco broccoli and in the arrangement of coniferous conifers.”
Once the basic concepts are understood, we can enter into programming. I have seen a hundred Python scripts on this topic, some using 4 or 5 lines, others more than 10. With loop structures: for, while, or simply using variables like "last =, penultimate =" etc.
All codes made in Python reach the result satisfactorily, however, the optimization is not the same in all cases. Personally, I am in a dilemma; Not having an acceptable command of Python, my reasoning for writing the code boils down to a very vertical procedure, typical of a beginner like me - no loops or loops and other gimmicks.
This was going to result in a long and boring script that would horrify more than one "Pythonian", so I decided to use an existing code and modify it minimally. The code in question is part of the official python page.
But first, we are going to install Python in case it is not present in our system:
Note: Use the appropriate command for your distribution.
Debian GNU / Linux and derivatives:
sudo apt install python3
Alternatively, we can install the Python development environment:
sudo apt install idle3
Let's do it
The code displayed on the Python site looks like this:
def fib (n):
a, b = 0,1
while a <n:
print (a, end = '')
a, b = b, a + b
As a result, the Fibonacci sequence :
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
What can be interpreted here:
In the first line a function fib is defined with a parameter n which will take a given value. Then two local variables whose values are 0 and 1 are defined; As explained above, these values must be used as defaults to be a Fibonacci sequence, the variables could also have been assigned in the following way - more usual in a novice -:
def fib (n):
a = 0
b = 1
From here a condition is used, in this case using the while loop. Python evaluates the condition and, if true ( True ), - executes the instructions contained in the body of the loop. This process is repeated as many times as the condition is true. When the condition returns false ( False ) —that is, it is not fulfilled— the body of the loop will finish executing and will continue with the rest of the instructions.
while a <n:
print (a, end = '')
a, b = b, a + b
It will print the values thrown by the variable a repeatedly as long as the condition is a <n, that is, true ( True ) when it verifies that a = n the condition will change to false ( False ) ending the execution of the program. The values of a and b are obtained using parallel assignment, I am not used to this instruction yet. The line that prints the values of the Fibonacci sequence is print (an end = '').
And finally, in the first line, we saw that the function def fib (n) whose parameter n will take an assigned value: 1000 in this case, but it can be any value that we assign to it, this is represented in the last line of code. This is where the function we create is called:
This code as it is presented was not attractive to me, although it achieves the purpose it was something strange for me. Suppose I wanted 18 terms of the sequence instead of 17 as the script is proposed, obviously, the next value to ... 987 is greater than 1000 - that's why it ended there -, vastly add 987 with the previous one to have a vague idea of the next finished; but what if I want more terms? 25, 30. I would be guessing all the time until I found the number of terms I want.
For 18 terms the value that follows is 1597, so any value between 1598 and 2584 will give that number:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
This method allows you to choose a maximum limit of the sequence, for example, if you want your final number not to be greater than 10000 then you use fib (10001). Although the returned value will be 6765, however, the number of terms cannot be specified for now.
See part two for better understanding….