Dados dos números n y m. Encuentre el entero más grande a(mcd), tal que todos los enteros n, n + 1, n + 2, …, m sean divisibles por a.
Ejemplos:
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.
Aquí, tenemos que examinar sólo dos casos:
- si a = b : el segmento consta de un solo número, por lo que la respuesta es a.
- si a < b : tenemos mcd(n, n + 1, n?+ 2, …, m) = mcd(mcd(n, n + 1), n + 2, …, m) = mcd(1, n + 2, …, n) = 1.
C++
// GCD of given range #include <bits/stdc++.h> 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.
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
<?php // PHP program for // GCD of given range // function returns the GCD function rangeGCD($n, $m) { return ($n == $m)? $n : 1; } // Driver Code $n = 475; $m = 475; echo rangeGCD($n, $m); // This code is contributed by anuj_67. ?>
Javascript
<script> // GCD of given range function rangeGCD( n, m) { return (n == m) ? n : 1; } var n = 475; var m = 475; document.write(rangeGCD(n, m)); </script>
Producción:
475
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por Abhishek Sharma 44 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA