Challenge: Euclidean Algorithm

Calculate the greatest common divisor using the Euclidean algorithm.

Euclidean algorithm

The Euclidean algorithm is a technique used to compute the greatest common divisor (GCD) of two numbers, i.e., the largest number that divides both of them without leaving a remainder.

Problem statement

Given two integers, x and y, calculate the largest number that divides both of them without leaving a remainder.

Input

Two integers: x and y.

Output

An integer that will be the GCD of x and y.

Sample input

x = 1071;
y = 462;

Sample output

result = 21;

Coding challenge

First, take a close look at this problem and design a step-by-step algorithm before jumping to the implementation. This problem is designed for your practice, so try to solve it on your own first. If you get stuck, you can always refer to the solution provided in the solution section. Good luck!

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.