--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 **.
--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)
#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))
A= 84 B= 60
A= 24 B= 60
A= 24 B= 36
A= 24 B= 12
Execution result: 12
――It was better to actually write the program than to just think about it. ――Next time, let's write a leap year algorithm
--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