Given two integers **L** and **R**, the task is to find the greatest divisor that divides all the natural numbers in the range **[L, R]**.

**Examples:**

Input:L = 3, R = 12

Output:1

Input:L = 24, R = 24

Output:24

**Approach:** For a range of consecutive integer elements, there are two cases:

- If
**L = R**then the answer will**L**. - If
**L < R**then all consecutive natural numbers in this range are co-primes. So,**1**is the only number that will be able to divide all the elements of the range.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the greatest divisor that ` `// divides all the natural numbers in the range [l, r] ` `int` `find_greatest_divisor(` `int` `l, ` `int` `r) ` `{ ` ` ` `if` `(l == r) ` ` ` `return` `l; ` ` ` ` ` `return` `1; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `l = 2, r = 12; ` ` ` ` ` `cout << find_greatest_divisor(l, r); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` ` ` `class` `GFG { ` ` ` `// Function to return the greatest divisor that ` `// divides all the natural numbers in the range [l, r] ` ` ` `static` `int` `find_greatest_divisor(` `int` `l, ` `int` `r) { ` ` ` `if` `(l == r) { ` ` ` `return` `l; ` ` ` `} ` ` ` ` ` `return` `1` `; ` ` ` `} ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) { ` ` ` `int` `l = ` `2` `, r = ` `12` `; ` ` ` ` ` `System.out.println(find_greatest_divisor(l, r)); ` ` ` `} ` `} ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the greatest divisor that ` `# divides all the natural numbers in the range [l, r] ` `def` `find_greatest_divisor(l, r): ` ` ` ` ` `if` `(l ` `=` `=` `r): ` ` ` `return` `l; ` ` ` ` ` `return` `1` `; ` ` ` ` ` `# Driver Code ` ` ` `l ` `=` `2` `; ` `r ` `=` `12` `; ` ` ` `print` `(find_greatest_divisor(l, r)); ` ` ` `#This code is contributed by Shivi_Aggarwal ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to return the greatest divisor that ` ` ` `// divides all the natural numbers in the range [l, r] ` ` ` `static` `int` `find_greatest_divisor(` `int` `l, ` `int` `r) { ` ` ` `if` `(l == r) { ` ` ` `return` `l; ` ` ` `} ` ` ` ` ` `return` `1; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() { ` ` ` `int` `l = 2, r = 12 ; ` ` ` ` ` `Console.WriteLine(find_greatest_divisor(l, r)); ` ` ` `} ` ` ` `// This code is contributed by Ryuga ` ` ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the greatest ` `// divisor that divides all the natural ` `// numbers in the range [l, r] ` `function` `find_greatest_divisor(` `$l` `, ` `$r` `) ` `{ ` ` ` `if` `(` `$l` `== ` `$r` `) ` ` ` `return` `$l` `; ` ` ` ` ` `return` `1; ` `} ` ` ` `// Driver Code ` `$l` `= 2; ` `$r` `= 12; ` ` ` `echo` `find_greatest_divisor(` `$l` `, ` `$r` `); ` ` ` `// This code is contributed by jit_t ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

1

**Time Complexity:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Sum of greatest odd divisor of numbers in given range
- Program to find Greatest Common Divisor (GCD) of N strings
- Find two co-prime integers such that the first divides A and the second divides B
- Count of triplets (a, b, c) in the Array such that a divides b and b divides c
- Find the k-th smallest divisor of a natural number N
- Count of numbers from the range [L, R] which contains at least one digit that divides K
- Find an integer X which is divisor of all except exactly one element in an array
- Highest power of 2 that divides the LCM of first N Natural numbers.
- Count of prime digits of a Number which divides the number
- Largest odd divisor Game to check which player wins
- Queries to find Kth greatest character in a range [L, R] from a string with updates
- Find elements in a given range having at least one odd divisor
- Smallest integer > 1 which divides every element of the given array
- Find a distinct pair (x, y) in given range such that x divides y
- Count of natural numbers in range [L, R] which are relatively prime with N
- Minimum divisor of a number to make the number perfect cube
- Largest divisor of a number not divisible by another given number
- Check if a M-th fibonacci number divides N-th fibonacci number
- Highest power of a number that divides other number
- Find element in array that divides all array elements

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.