Created
February 24, 2015 18:17
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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