This content originally appeared on DEV Community and was authored by Dhruva Srinivas
Generating a Fibonacci sequence is an extremely popular programming question, which might seem intimidating at first but it's actually quite simple.
For those of you boomers who don't know what a Fibonacci sequence is:
In mathematics, the Fibonacci numbers, commonly denoted Fₙ, form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, and for n > 1.
So what we're gonna do is programmatically generate an N length of Fibonacci numbers.
Enter the Fibonacci length: 8
[0, 1, 1, 2, 3, 5, 8, 13]
Visualizing a way to solve the problem
A good approach to solving these questions would be to break 'em up into little tidbits and understanding the problem and then implement that in code. Here, these are the steps
- Get user input for the length of the Fibonacci sequence.
- Set initial values of the sequence. (This is always 0 and 1)
- Create a loop and check if the length of the sequence is equal to the number the user has specified. If it is, then exit the loop. If it isn't generate the Fibonacci number and append that to the list of already generated Fibonacci numbers.
- And finally, print the Fibonacci sequence. But, how in the world do you generate the Fibonacci number??
So if you noticed, Fibonacci numbers are basically the sum of the previous two numbers in the sequence. And if you didn't, well that just sucks. Anyway, what we basically wanna do is
- Access the last two elements of the sequence/list
- Add 'em up
- Append them to the sequence/list
WOAH, so that's it? Yup.
Let's get started coding!
Solving the problem
So, I'm gonna do this in Python, you can choose whatever language you want, the concept is dead simple and you should be able to implement this in any language.
- First, we wanna get the user input, so that's nice & straightforward.
# inp is short for input
inp = int(input("Enter the length of the Fibonacci sequence: "))
- Now we'll create a function that generates the Fibonacci sequence:
inp = int(input("Enter the length of the Fibonacci sequence: "))
def fib(fibonacci_length):
pass
- We can create a list and populate it with the default values of a Fibonacci sequence:
inp = int(input("Enter the length of the Fibonacci sequence: "))
def fib(fibonacci_length):
fibonacci_list = [0, 1]
- Now, we're gonna create a
while
loop which checks if the Fibonacci length is equal to the length of the listfibonacci_list
or not.
inp = int(input("Enter the length of the Fibonacci sequence: "))
def fib(fibonacci_length):
fibonacci_list = [0, 1]
while len(fibonacci_list) != fibonacci_length:
pass
- We're now going to access the last two values of
fibonacci_list
by grabbing their indexes:
inp = int(input("Enter the length of the Fibonacci sequence: "))
def fib(fibonacci_length):
fibonacci_list = [0, 1]
while len(fibonacci_list) != fibonacci_length:
second_last_index = len(fibonacci_list) - 2
last_index = len(fibonacci_list) - 1
- Now we can append the sum of the last two values of the list to
fibonacci_list
and print the whole list:
inp = int(input("Enter the length of the Fibonacci sequence: "))
def fib(fibonacci_length):
fibonacci_list = [0, 1]
while len(fibonacci_list) != fibonacci_length:
second_last_index = len(fibonacci_list) - 2
last_index = len(fibonacci_list) - 1
fibonacci_list.append(fibonacci_list[second_last_index] + fibonacci_list[last_index])
print(fibonacci_list)
- And finally, we're gonna call the
fib
function and pass the user input (inp
) variable to thefibonacci_length
parameter:
inp = int(input("Enter the length of the Fibonacci sequence: "))
def fib(fibonacci_length):
fibonacci_list = [0, 1]
while len(fibonacci_list) != fibonacci_length:
second_last_index = len(fibonacci_list) - 2
last_index = len(fibonacci_list) - 1
fibonacci_list.append(fibonacci_list[second_last_index] + fibonacci_list[last_index])
print(fibonacci_list)
fib(inp)
And that's all there is to it!
If you have coded this in a different programming language please share it down below :)
Thanks for reading, I'll catch you guys in my next post!
This content originally appeared on DEV Community and was authored by Dhruva Srinivas
Dhruva Srinivas | Sciencx (2021-05-11T14:57:00+00:00) Common Programming Questions: The Fibonacci Sequence. Retrieved from https://www.scien.cx/2021/05/11/common-programming-questions-the-fibonacci-sequence/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.