Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class klausur {
- public static int eingabegeradezahl() {
- int min=3;
- int n;
- Scanner sc= new Scanner(System.in);
- do { System.out.print("Bitte geben sie eine gerade Zahl ein.");
- n=sc.nextInt();
- if((n<min) || (n%2 == 1)) System.out.println(n+" ist eine ungΓΌltige Eingabe");
- } while ((n<min) || (n%2 == 1)); // end of do-while loop
- return n;
- } // end of eingabegeradezahl
- public static void goldbachzerlegung(int m, int n) {
- int x;
- if (m>0) {
- if ((n-m)>0) {
- x=n-m;
- if (testprim(x)==1) {
- System.out.println(m+" und "+x+" ergeben zusammen "+n);
- } // end of if
- } //end of if
- } // end of if
- } // end of goldbachzerlegung
- public static int testprim(int n)
- {
- int prim=1;
- int t=1;
- do {
- t++;
- if ( (n%t) ==0) {
- prim=0;
- } // end of if
- } while ( (prim==1) && (t<(n-1)) ); // end of do-while loop
- return prim;
- } //end of testprim
- public static void main(String[] args) {
- int pmax=1000;
- int m;
- int i=0;
- int[] p = new int[pmax];
- int n;
- p[i]=2; i++;
- for(m=3; m<pmax; m=m+2) {
- if (testprim(m)==1) {
- p[i]=m;
- i++;
- } // end of if
- } // end of for-loop
- n=eingabegeradezahl();
- for (m=0; m<i; m++) goldbachzerlegung(p[m], n);
- } // end of main
- } // end of class klausur
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement