Fibonacci sequence in Python
The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones, starting from 0 and 1. In other words, each number in the series is the sum of the previous two numbers.
The sequence starts as follows:
As you can see, each number is the sum of the two preceding ones. For example, 1+1=2, 2+1=3, 3+2=5, and so on. The Fibonacci series has many interesting mathematical properties and occurs frequently in nature and in various fields of study, such as mathematics, biology, and economics.
Fibonacci series using recursion
Following an example of a recursive function to generate the Fibonacci sequence in Python (not using any loop):
Above function uses recursion to calculate the nth number in the Fibonacci sequence by summing the previous two numbers.
Fibonacci series using Dynamic Programming
Following is an example of a function to generate the Fibonacci series using dynamic programming in Python:
Above function uses dynamic programming to avoid redundant calculations by storing previously computed Fibonacci numbers in a list and reusing them as needed.
Time complexity:
The time complexity of this function is O(n), which is linear. This is because the function iterates n-1 times using the for loop to compute the nth Fibonacci number, and each iteration requires only constant time operations to access and update the list of previously computed numbers.
Space complexity
The space complexity of this function is O(n), which is linear. This is because the function creates a list of length n+1 to store the previously computed Fibonacci numbers. The space used by the list grows linearly with the input size n. However, we could improve the space complexity by only storing the last two Fibonacci numbers instead of the entire sequence. This would reduce the space complexity to O(1).
Fibonacci series using Space Optimized
Following is an example of a space-optimized function to generate the Fibonacci sequence in Python:
Above function is the same as the for loop version provided earlier, but it uses only two variables (a and b) to keep track of the previous two Fibonacci numbers instead of a list.
Time complexity:
The time complexity of this function is O(n), which is linear. This is because the function iterates n-1 times using the for loop to compute the nth Fibonacci number.
Space complexity
The space complexity of this function is O(1), which is constant. This is because the function only uses two variables (a and b) to keep track of the previous two Fibonacci numbers, and the space used by the variables does not depend on the input size n. This is the most space-efficient way to compute the Fibonacci sequence, using constant space.
Fibonacci series using while loop
Following is an example of a function to generate the Fibonacci sequence using a while loop in Python (not using recursion):
Above function iteratively calculates the nth number in the Fibonacci sequence by adding the previous two numbers using a while loop.
Time complexity:
The time complexity of this function is O(n), which is linear. This is because the function iterates n-1 times using the while loop to compute the nth Fibonacci number.
Space complexity
The space complexity of this function is O(1), which is constant. This is because the function only uses three variables (a, b, and c) to keep track of the previous two Fibonacci numbers and the current Fibonacci number. The space used by the variables does not depend on the input size n.
Fibonacci series using Backtracking
Backtracking is not typically used to generate the Fibonacci sequence, as it is not an efficient approach for this particular problem. However, in theory, you could use backtracking to generate the Fibonacci sequence by exploring all possible combinations of Fibonacci numbers until you find the nth number.
Therefore, while it is possible to use backtracking to generate the Fibonacci sequence, it is not a practical or efficient approach for this problem.
- Python print statement "Syntax Error: invalid syntax"
- Installing Python Modules with pip
- How to get current date and time in Python?
- No module named 'pip'
- How to get the length of a string in Python
- ModuleNotFoundError: No module named 'sklearn'
- ModuleNotFoundError: No module named 'cv2'
- Python was not found; run without arguments
- Attempted relative import with no known parent package
- TypeError: only integer scalar arrays can be converted to a scalar index
- A value is trying to be set on a copy of a slice from a DataFrame
- ValueError: setting an array element with a sequence
- Indentationerror: unindent does not match any outer indentation level
- Valueerror: if using all scalar values, you must pass an index
- ImportError: libGL.so.1: cannot open shared object file: No such file or directory
- Python Try Except | Exception Handling
- Custom Exceptions in Python with Examples
- Python String replace() Method
- sqrt Python | Find the Square Root in Python
- Read JSON file using Python
- Binary search in Python
- Defaultdict in Python
- Int Object is Not Iterable – Python Error
- os.path.join in Python
- TypeError: int object is not subscriptable
- Python multiline comment
- Typeerror: str object is not callable
- Python reverse List
- zip() in Python for Parallel Iteration
- strftime() in Python
- Typeerror: int object is not callable
- Python List pop() Method
- Python any() function
- Python any() Vs all()
- Python pass Statement
- Python Lowercase - String lower() Method
- Modulenotfounderror: no module named istutils.cmd
- Append to dictionary in Python : Key/Value Pair
- timeit | Measure execution time of small code
- Python Decimal to Binary
- GET and POST requests using Python
- Difference between List VS Set in Python
- How to Build Word Cloud in Python?
- Binary to Decimal in Python
- Modulenotfounderror: no module named 'apt_pkg'
- Convert List to Array Python