Array ( [0] => [1] => questions [2] => Popular-Algorithms [3] => EUCLIDIAN-GCD-ALGORITHM ) Popular Algorithms | EUCLIDIAN GCD ALGORITHM | THE INQUISITIVE





EUCLIDIAN GCD ALGORITHM

LEVEL:Beginner

Description

Given two Integers. Find the GCD of the integers using Euclidian GCD Algorithm.

Input Format

2 integers separated by spaces.

Output Format

Print the Greatest Common Divisor.


Example 1:

Input
20 30
Output
5
Example 2:

Input
21 31
Output
1
Example 3:

Input
788 488
Output
4

oops

Login to see Discussion




Approach

Step-i) Let a, b be the two numbers
Step-ii) a mod b = R
Step-iii) Let a = b and b = R
Step-iv) Repeat Steps 2 and 3 until a mod b is greater than 0
Step-v) GCD = b

Time Complexity: O(log n)
Space Complexity: O(1)


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution