Dados dos enteros L y R , la tarea es encontrar el mayor divisor que divida a todos los números naturales en el rango [L, R] .
Ejemplos:
Entrada: L = 3, R = 12
Salida: 1
Entrada: L = 24, R = 24
Salida: 24
Enfoque: para un rango de elementos enteros consecutivos, hay dos casos:
- Si L = R entonces la respuesta será L .
- Si L < R , todos los números naturales consecutivos en este rango son coprimos. Entonces, 1 es el único número que podrá dividir todos los elementos del rango.
A continuación se muestra la implementación del enfoque anterior:
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); }
C
// C implementation of the approach #include <stdio.h> // 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; printf("%d",find_greatest_divisor(l, r)); } // This code is contributed by kothvvsaakash.
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>
Producción:
1
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)