# Greatest divisor which divides all natural number in range [L, R]

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 = 12Output:1Input:L = 24, R = 24Output:24

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the

DSA Self Paced Courseat a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please referComplete Interview Preparation Course.In case you wish to attend

live classeswith experts, please referDSA Live Classes for Working ProfessionalsandCompetitive Programming Live for Students.

**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);` `}` |

## 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` |

## 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` |

## 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` `}` |

## 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` `?>` |

## Javascript

`<script>` `// Javascript 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` `let l = 2;` `let r = 12;` `document.write( find_greatest_divisor(l, r));` `// This code is contributed` `// by bobby` `</script>` |

**Output:**

1

**Time Complexity:** O(1)