Challenge: Euclidean Algorithm

In this lesson, we will introduce the euclidean algorithm for calculating the greatest common divisor and solve a challenge on it.

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 which will be x and y's GCD

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 70+ hands-on prep courses.