Python memorandum (algorithm)

A memorandum to relearn Python from the ground up. I would like to achieve results that are tackling basic problems.

Fibonacci sequence

Create a result display program for the Fibonacci sequence. By the way, the formula is

a_{1} = a_{2} = 1 \\ 
a_{n+2} = a_{n+1} + a_{n}\;\;\;\;\;(n >= 1) 

Is.

Write as official

fibonacci_simple.py


#From the Fibonacci sequence formula, it is as follows.
#At this time, the fibonacci function is called many times to get the numerical value of n.
#Therefore, the algorithm is simple, but the processing is slow.
import time 


def fibonacci_simple(n):
    if (n == 1) or (n == 2):
        return 1
    return fibonacci_simple(n - 2) + fibonacci_simple(n - 1)


start = time.time()
print(fibonacci_simple(20))
elapsed_time = time.time() - start
print ("elapsed_time:{0}".format(elapsed_time) + "[sec]")

as a result

6765
elapsed_time:0.002089977264404297[sec]

Try to make it a high-speed process with memoization

fibonacci_memo.py


#Speed up processing by memoizing.
#The value of the conditional array is put in the associative array called memo.
#memo If there is always, it ends. If not, calculate and insert into memo
#The same calculation can be eliminated.
import time


memo = {1: 1, 2: 1}
def fibonacci_memo(n):
    if n in memo:
        return memo[n]
    memo[n] = fibonacci_memo(n - 2) + fibonacci_memo(n - 1)
    return memo[n]


start = time.time()
print(fibonacci_memo(20))
elapsed_time = time.time() - start
print ("elapsed_time:{0}".format(elapsed_time) + "[sec]")

as a result

6765
elapsed_time:0.0002808570861816406[sec]

Try using a loop

fibonacci_loop.py


#Use a loop to find it.
import time


def fibonacci_loop(n):
    fib = [1, 1]
    for i in range(2, n):
        fib.append(fib[i -2] + fib[i - 1])
        
    return fib[n - 1]


start = time.time()
print(fibonacci_loop(20))
elapsed_time = time.time() - start
print ("elapsed_time:{0}".format(elapsed_time) + "[sec]")

as a result

6765
elapsed_time:0.00026702880859375[sec]

If you compare

It's amazing that the processing time can be reduced to 1/10 just by reducing the amount of duplicate calculation compared to the simple one.

Recommended Posts

Python memorandum (algorithm)
Python memorandum
Python Memorandum 2
Python algorithm
Python memorandum
python memorandum
python memorandum
Python memorandum
python memorandum
Python memorandum
Python basics memorandum
Python pathlib memorandum
Python memorandum [links]
Python memorandum numbering variables
A * algorithm (Python edition)
Python basic grammar / algorithm
python memorandum (sequential update)
Genetic algorithm in python
Python memorandum (personal bookmark)
Python basic memorandum part 2
Algorithm in Python (Bellman-Ford)
Relearn Python (Algorithm I)
[Python] Iterative processing_Personal memorandum
Memorandum @ Python OR Seminar
python memorandum super basic
Algorithm in Python (Dijkstra's algorithm)
Effective Python Learning Memorandum Day 15 [15/100]
Cisco Memorandum _ Python config input
Effective Python Learning Memorandum Day 6 [6/100]
Algorithm in Python (primality test)
Effective Python Learning Memorandum Day 12 [12/100]
Effective Python Learning Memorandum Day 9 [9/100]
Effective Python Learning Memorandum Day 8 [8/100]
ABC memorandum [ABC163 C --managementr] (Python)
About python beginner's memorandum function
Memorandum @ Python OR Seminar: matplotlib
[Python] SQLAlchemy error avoidance memorandum
Search algorithm using word2vec [python]
Reproduce Euclidean algorithm in Python
Algorithm in Python (binary search)
A memorandum about correlation [Python]
Effective Python Learning Memorandum Day 14 [14/100]
Effective Python Learning Memorandum Day 1 [1/100]
[Python3] Dijkstra's algorithm with 14 lines
Memorandum @ Python OR Seminar: Pulp
Effective Python Learning Memorandum Day 13 [13/100]
A memorandum about Python mock
Effective Python Learning Memorandum Day 3 [3/100]
Effective Python Learning Memorandum Day 5 [5/100]
Memorandum @ Python OR Seminar: Pandas
[python] Random number generation memorandum
Implement Dijkstra's Algorithm in python
Effective Python Learning Memorandum Day 4 [4/100]
Memorandum @ Python OR Seminar: scikit-learn
Effective Python Learning Memorandum Day 7 [7/100]
Effective Python Learning Memorandum Day 2 [2/100]
python parallel / asynchronous execution memorandum
Algorithm in Python (breadth-first search, bfs)
ABC memorandum [ABC159 C --Maximum Volume] (Python)
Python pywin32 (win32com) Excel operation memorandum
Sorting algorithm and implementation in Python