[Fundamental Information Technology Engineer Examination] I wrote the algorithm of Euclidean algorithm in Python.

Overview

--There is an algorithm in the afternoon exam of the Fundamental Information Technology Engineer Examination. I can't understand even if I solve the past questions ... I would like to actually write the algorithm in Python to deepen my understanding.

――First of all, let's write from the algorithm of ** Euclidean algorithm **.

Euclidean algorithm

algorithm

--Repeat subtracting the smaller of the two integers from the larger one until they are equal. The equalized value is the greatest common divisor. --GCM is the Greatest Common Measure (greatest common divisor)

code

#GCM function to find the greatest common divisor by Euclidean algorithm
def GCM(A,B):
    #Iterative processing
    while A != B: #Repeat until A and B are equal
        print("A=",A,"B=",B) #Results on the way
        #Branch processing
        if A > B: #If A is greater than B
            A = A - B #A to A-Store B
        else:
            B = B - A #B to B-Store A
    return A

print("Execution result:",GCM(84,60))

Execution result

A= 84 B= 60
A= 24 B= 60
A= 24 B= 36
A= 24 B= 12
Execution result: 12

Summary

――It was better to actually write the program than to just think about it. ――Next time, let's write a leap year algorithm

reference

--I quoted or referred to Chapter 3 01 Euclidean algorithm in this book. Information processing textbook, book that can solve algorithm problems of the Fundamental Information Technology Engineer Examination, 2nd edition

Recommended Posts

[Fundamental Information Technology Engineer Examination] I wrote the algorithm of Euclidean algorithm in Python.
[Fundamental Information Technology Engineer Examination] I wrote an algorithm for the maximum value of an array in Python.
[Fundamental Information Technology Engineer Examination] I wrote a linear search algorithm in Python.
[Fundamental Information Technology Engineer Examination] I wrote an algorithm for determining leap years in Python.
A story about downloading the past question PDF of the Fundamental Information Technology Engineer Examination in Python at once
python Basic sorting algorithm summary (Basic Information Technology Engineer Examination)
Experience of taking the Applied Information Technology Engineer Examination
I tried it with Wolfram Alpha and google, referring to "[Fundamental Information Technology Engineer Examination] I wrote an algorithm for determining leap years in Python."
I wrote the queue in Python
I wrote the stack in Python
I wrote the code to write the code of Brainf * ck in python
Fundamental Information Technology Engineer Examination (FE) Afternoon Exam Python Sample Question Explanation
Note that I understand the algorithm of the machine learning naive Bayes classifier. And I wrote it in Python.
Note that I understand the least squares algorithm. And I wrote it in Python.
Fundamental Information Technology Engineer Examination Implemented Python sample questions without using external libraries
Basic information Write the 2018 fall algorithm problem in Python
Implemented the algorithm of "Algorithm Picture Book" in Python3 (Heapsort)
I wrote python in Japanese
Reproduce Euclidean algorithm in Python
I tried the accuracy of three Stirling's approximations in python
I wrote the basic grammar of Python with Jupyter Lab
I wrote the basic operation of Seaborn in Jupyter Lab
Implemented the algorithm of "Algorithm Picture Book" in Python3 (Bubble Sort)
I wrote the basic operation of Numpy in Jupyter Lab.
Implemented the algorithm of "Algorithm Picture Book" in Python3 (selection sort)
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
I wrote Fizz Buzz in Python
I compared the calculation time of the moving average written in Python
[Python] I wrote the route of the typhoon on the map using folium
Part 1 I wrote the answer to the reference problem of how to write offline in real time in Python
Check the behavior of destructor in Python
I didn't know the basics of Python
The result of installing python in Anaconda
I want to batch convert the result of "string" .split () in Python
I want to explain the abstract class (ABCmeta) of Python in detail.
I wrote the selection sort in C
The basics of running NoxPlayer in Python
The Python project template I think of.
In search of the fastest FizzBuzz in Python
[Python beginner] I collected the articles I wrote
I tried to get the movie information of TMDb API with Python
I made a program to check the size of a file in Python
I wrote the sliding wing in creation.
Non-recursive implementation of extended Euclidean algorithm (Python)
Part 1 I wrote an example of the answer to the reference problem of how to write offline in real time in Python
You will be an engineer in 100 days --Day 29 --Python --Basics of the Python language 5
I want to use Python in the environment of pyenv + pipenv on Windows 10
I installed Pygame with Python 3.5.1 in the environment of pyenv on OS X
You will be an engineer in 100 days --Day 33 --Python --Basics of the Python language 8
You will be an engineer in 100 days --Day 26 --Python --Basics of the Python language 3
I compared the speed of regular expressions in Ruby, Python, and Perl (2013 version)
You will be an engineer in 100 days --Day 32 --Python --Basics of the Python language 7
I searched for the skills needed to become a web engineer in Python
You will be an engineer in 100 days --Day 28 --Python --Basics of the Python language 4
Output the number of CPU cores in Python
[Python] Sort the list of pathlib.Path in natural sort
A memo that I wrote a quicksort in Python
Get the caller of a function in Python
Match the distribution of each group in Python
View the result of geometry processing in Python
I tried simulating the "birthday paradox" in Python