I tried recursion with Python ② (Fibonacci sequence)

Previously, I used recursion to judge palindromes and draw Fractal figures, but this time it is the second one.

Fibonacci sequence

What is the Fibonacci sequence? 1 1 2 3 5 8 13... It is a sequence in which the sum of the previous two numbers is the next term. (Example) 3 in item 4 is the sum of the previous two 1s and 2s. This time I wrote a program that uses recursion to know the nth term of the Fibonacci sequence.

def fibonacci(n):
    if n < 3:
        return 1
    else:
        return fibonacci(n-1) + fibonacci(n-2)

If it is one or two terms, it is one, and after that, it goes back to the previous and two terms, and then goes back one and two. It's a little confusing, If n = 5, the sum of the 4th and 3rd terms is required, so go back to the 2nd and 1st terms to find the sum. This gives 55 for n = 10 and 6765 for n = 20 (the larger the number, the longer it takes).

Recommended Posts

I tried recursion with Python ② (Fibonacci sequence)
I tried fp-growth with python
I tried scraping with Python
I tried gRPC with Python
I tried scraping with python
I tried to study DP with Fibonacci sequence
I tried web scraping with python.
I tried running prolog with python 3.8.2.
I tried SMTP communication with Python
Algorithm learned with Python 5th: Fibonacci sequence
I tried sending an email with python.
I tried non-photorealistic rendering with Python + opencv
I tried a functional language with Python
#I tried something like Vlookup with Python # 2
Fibonacci sequence using Python
I tried Python> autopep8
I tried Python> decorator
I tried "smoothing" the image with Python + OpenCV
I tried hundreds of millions of SQLite with python
I tried "differentiating" the image with Python + OpenCV
I tried L-Chika with Raspberry Pi 4 (Python edition)
I tried Jacobian and partial differential with python
I tried to get CloudWatch data with Python
I tried using mecab with python2.7, ruby2.3, php7
I tried function synthesis and curry with python
I tried "binarizing" the image with Python + OpenCV
I tried running faiss with python, Go, Rust
I tried to automate sushi making with python
I tried playing mahjong with Python (single mahjong edition)
I tried running Deep Floor Plan with Python 3.6.10.
I tried sending an email with SendGrid + Python
I tried Learning-to-Rank with Elasticsearch!
I tried to implement Minesweeper on terminal with python
I tried to get started with blender python script_Part 01
I made blackjack with python!
I tried to touch the CSV file with Python
I tried to draw a route map with Python
[OpenCV / Python] I tried image analysis of cells with OpenCV
I tried clustering with PyCaret
I tried to solve the soma cube with python
I tried to get started with blender python script_Part 02
I tried to implement an artificial perceptron with python
I tried to automatically generate a password with Python3
I tried Python C extension
[Python] I tried using OpenPose
Mayungo's Python Learning Episode 1: I tried printing with print
I tried to solve the problem with Python Vol.1
I tried to analyze J League data with Python
I made blackjack with Python.
I made wordcloud with Python.
I tried "morphology conversion" of images with Python + OpenCV
I tried hitting the API with echonest's python client
I tried to solve AOJ's number theory with Python
I tried deploying Kubernetes Pods / Helm Chart with Pulumi (Python)
I tried to find the entropy of the image with python
I tried to simulate how the infection spreads with Python
I tried using the Python library from Ruby with PyCall
I tried to make various "dummy data" with Python faker
I tried to find the average of the sequence with TensorFlow
I tried various methods to send Japanese mail with Python
I tried sending an email from Amazon SES with Python