Skip to content

Instantly share code, notes, and snippets.

@heckenmann
Created February 24, 2015 18:17
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 heckenmann/978a643c8437a9ffaffc to your computer and use it in GitHub Desktop.
Save heckenmann/978a643c8437a9ffaffc to your computer and use it in GitHub Desktop.
Euklidischer Algorithmus: Der folgende Code berechnet den ggT (größter gemeinsamer Teiler) nach dem euklidischen Algorithmus
public class EA {
public static void main(String[] args) {
for(int i = 0; i < 1000; i++) {
for(int j = 0; j < 1000; j++) {
if(ggTIterativ(i,j) != ggT(i,j))
System.out.println(i + "" + j);
}
}
}
// Berechnet den ggT durch Rekursion
public static int ggT(int a, int b) {
if(b == 0)
return a;
return ggT(b, a%b);
}
// Berechnet den ggT durch Iteration
public static int ggTIterativ(int a, int b) {
while(b != 0) {
int c = a;
a = b;
b = c % b;
}
return a;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment