GCD of two numbers

Beginner Problems Basic Maths Easy
  • Here's a fun fact: the concept of finding the Greatest Common Divisor (GCD) is used in real-world encryption algorithms, particularly in RSA encryption
  • RSA, which stands for Rivest-Shamir-Adleman, is a public key encryption technology used widely in securing sensitive data, especially when being sent over an insecure network
  • It involves the use of large prime numbers, and the computation of their GCD, to generate secure public and private keys
  • So, the algorithm to solve this programming problem is integral to promoting online security and privacy

You are given two integers n1 and n2. You need find the Greatest Common Divisor (GCD) of the two given numbers. Return the GCD of the two numbers.


The Greatest Common Divisor (GCD) of two integers is the largest positive integer that divides both of the integers.

Examples:

Input: n1 = 4, n2 = 6

Output: 2

Explanation: Divisors of n1 = 1, 2, 4, Divisors of n2 = 1, 2, 3, 6

Greatest Common divisor = 2.

Input: n1 = 9, n2 = 8

Output: 1

Explanation: Divisors of n1 = 1, 3, 9 Divisors of n2 = 1, 2, 4, 8.

Greatest Common divisor = 1.

Input: n1 = 6, n2 = 12

Constraints

  • 1 <= n1, n2 <= 1000

Company Tags

TCS Cognizant Accenture Infosys Capgemini Wipro IBM HCL Tech Mahindra MindTree