Reproduce Euclidean algorithm in Python

Introduction

This is a memorandum by a super beginner

This is my first post. It's been a week since I started doing python. I'm still a chick, so I'm practicing with the problem collection I found online.

I will leave an article as a memorandum because the problem I found at that time could not be solved well.

This is a must-see for super beginners! !! !!

What is Euclidean algorithm?

Simply put, a calculation method that can mechanically obtain the greatest common divisor.

Method of calculation ① Prepare two natural numbers you want to check ② Large ÷ small (3) If there is a remainder, the smaller number and its greatest common divisor match the greatest common divisor of the original two numbers. ④ By repeating this, you can find the greatest common divisor with simple numbers.

Well it looks like this. I'm sorry it's hard to understand.

Actually try

Now let's write the code. First, define a new function gcd ()

def gcd(a,b) #Define function

Use def to define the function yourself.

Next, let's write the processing that this function does. In Euclidean algorithm, the calculation has to be repeated many times. Therefore, the following conditional branching is required.

① When there is no remainder (when it is divisible), the calculation ends there. ② If there is a remainder, continue the calculation

The processing is divided according to the two cases. In other words, you should use if!

Therefore, I think the code will look like the one below.

def gcd(a,b) #Define function
#At the time of ①
    if b == 0
        return a
#At the time of ②
    else:
        return gcd(b,a%b)

This is the end of writing! !! !! After that, it seems that you should use the input function and add the print function to output the result so that you can freely input your favorite 2 numbers. ↓ is the completed form.

a,b = input(),input()

#Convert to integer
a,b = int(a),int(b)

#Define function
def gcd(a,b) 
#At the time of ①
    if b == 0
        return a
#At the time of ②
    else:
        return gcd(b,a%b)
#Output result
print(gcd(a,b))

That's all there is to it! !!

Summary

This time, the Euclidean algorithm is expressed in python.

At first, the input didn't work and I got an error, but when I attached the int, it worked. ~~ I'm not sure why, so I'd be happy if you could tell me about that. ~~

Thank you for reading until the end! !! !! Feel free to give me any advice!

Recommended Posts

Reproduce Euclidean algorithm in Python
Algorithm in Python (Bellman-Ford)
Algorithm in Python (Dijkstra's algorithm)
Algorithm in Python (binary search)
Implement Dijkstra's Algorithm in python
Python algorithm
Algorithm in Python (breadth-first search, bfs)
Sorting algorithm and implementation in Python
Write A * (A-star) algorithm in Python
Develop an investment algorithm in Python 2
Algorithm in Python (depth-first search, dfs)
Implementing a simple algorithm in Python 2
Algorithm (segment tree) in Python (practice)
Run a simple algorithm in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Python memorandum (algorithm)
Unittest in python
Ant book in python: Sec.2-5 Dijkstra's algorithm
Algorithm in Python (ABC 146 C Binary Search
Epoch in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Write a simple greedy algorithm in Python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Alignment algorithm by insertion method in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
Non-recursive implementation of extended Euclidean algorithm (Python)
LiNGAM in Python
Flatten in python