Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created August 24, 2020 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/6a7acac4a0daa815f6c8c21069c7d16f to your computer and use it in GitHub Desktop.
Save parzibyte/6a7acac4a0daa815f6c8c21069c7d16f to your computer and use it in GitHub Desktop.
public class Main {
// https://parzibyte.me/blog
public static void main(String[] args) {
int a = 10;
int b = 15;
int mcm = minimoComunMultiplo(a, b);
System.out.printf("El mínimo común múltiplo de %d y %d es %d\n", a, b, mcm);
}
// Vamos a necesitar esta fórmula para calcular el mínimo común múltiplo
public static int maximoComunDivisor(int a, int b) {
int temporal;//Para no perder b
while (b != 0) {
temporal = b;
b = a % b;
a = temporal;
}
return a;
}
public static int minimoComunMultiplo(int a, int b) {
// Y ahora aplicamos la fórmula que dice:
// MCM(a, b) = (a * b) / MCD(a, b)
return (a * b) / maximoComunDivisor(a, b);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment