# GCD of elements in a given range

Given two numbers n and m. Find the biggest integer a(gcd), such that all integers n, n + 1, n + 2, …, m are divisible by a.

Examples:

```Input : n = 1, m = 2
Output: 1
Explanation:
Here, series become 1, 2. So, the
greatest no which divides both of
them is 1.

Input : n = 475, m = 475
Output : 475
Explanation:
Here, series has only one term 475.
So, greatest no which divides 475 is 475.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Here, We have to examine only two cases:

1. if a = b : the segment consists of a single number, hence the answer is a.
2. if a < b : we have gcd(n, n + 1, n?+ 2, …, m) = gcd(gcd(n, n + 1), n + 2, …, m) = gcd(1, n + 2, …, n) = 1.
3. Below is the code for above approach.

## C++

 `// GCD of given range ` `#include ` `using` `namespace` `std; ` ` `  `int` `rangeGCD(``int` `n, ``int` `m) ` `{ ` `    ``return` `(n == m)? n : 1; ` `} ` ` `  `int` `main() ` `{ ` `    ``int` `n = 475; ` `    ``int` `m = 475; ` `    ``cout << rangeGCD(n, m); ` `    ``return` `0; ` `} `

## Java

 `// GCD of given range ` ` `  `import` `java.io.*; ` ` `  `class` `GFG { ` ` `  `    ``static` `int` `rangeGCD(``int` `n, ``int` `m) ` `    ``{ ` `        ``return` `(n == m) ? n : ``1``; ` `    ``} ` ` `  `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `n = ``475``; ` `        ``int` `m = ``475``; ` `         `  `        ``System.out.println(rangeGCD(n, m)); ` `    ``} ` `} ` ` `  `// This code is contributed by Ajit. `

/div>

## Python3

 `  `  `# GCD of given range ` ` `  `def` `rangeGCD(n, m): ` `    ``return` `n ``if``(n ``=``=` `m) ``else` `1` `     `  `# Driver code ` `n, m ``=` `475``, ``475` `print``(rangeGCD(n, m)) ` ` `  `# This code is contributed by Anant Agarwal. `

## C#

 `// GCD of given range ` `using` `System; ` `  `  `class` `GFG { ` `  `  `    ``static` `int` `rangeGCD(``int` `n, ``int` `m) ` `    ``{ ` `        ``return` `(n == m) ? n : 1; ` `    ``} ` `  `  `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 475; ` `        ``int` `m = 475; ` `          `  `        ``Console.WriteLine(rangeGCD(n, m)); ` `    ``} ` `} ` `  `  `// This code is contributed by Anant Agarwal. `

## PHP

 ` `

Output:

```475
```

## tags:

Mathematical GCD-LCM Mathematical