Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created August 24, 2020 02:13
Show Gist options
  • Save parzibyte/67a17e1e0d4c698290ca5e4071a37720 to your computer and use it in GitHub Desktop.
Save parzibyte/67a17e1e0d4c698290ca5e4071a37720 to your computer and use it in GitHub Desktop.
public class Main {
public static void main(String[] args) {
int a = 50;
int b = 120;
int mcd = maximoComunDivisor(a, b);
System.out.printf("El MCD de %d y %d es %d\n", a, b, mcd);
int mcdRecursivo = maximoComunDivisorRecursivo(a, b);
System.out.printf("El MCD de %d y %d (con recursividad) es %d\n", a, b, mcdRecursivo);
}
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 maximoComunDivisorRecursivo(int a, int b) {
if (b == 0) return a;
return maximoComunDivisorRecursivo(b, a % b);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment