Advertisement
fkrone

Java vs Lua

Apr 28th, 2011
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class klausur {
  4.  
  5.         public static int eingabegeradezahl() {
  6.                         int min=3;
  7.                         int n;
  8.                        
  9.                         Scanner sc= new Scanner(System.in);
  10.                        
  11.                         do { System.out.print("Bitte geben sie eine gerade Zahl ein.");
  12.                                 n=sc.nextInt();
  13.                                 if((n<min) || (n%2 == 1)) System.out.println(n+" ist eine ungΓΌltige Eingabe");
  14.                                
  15.                         } while ((n<min) || (n%2 == 1));        // end of do-while loop
  16.                         return n;
  17.         }       // end of eingabegeradezahl
  18.        
  19.         public static void goldbachzerlegung(int m, int n) {
  20.                 int x;
  21.                
  22.                 if (m>0) {
  23.                         if ((n-m)>0) {
  24.                                 x=n-m;
  25.                                 if (testprim(x)==1) {
  26.                                         System.out.println(m+" und "+x+" ergeben zusammen "+n);
  27.                                 }       // end of if
  28.                                
  29.                         }       //end of if
  30.                 }       // end of if
  31.         }       // end of goldbachzerlegung
  32.        
  33.         public static int testprim(int n)
  34.         {                      
  35.                 int prim=1;
  36.                 int t=1;
  37.                        
  38.                         do {
  39.                                
  40.                                 t++;
  41.                                 if ( (n%t) ==0) {
  42.                                         prim=0;
  43.                                 } // end of if
  44.                         } while ( (prim==1) && (t<(n-1)) );     // end of do-while loop
  45.                 return prim;
  46.                
  47.         }       //end of testprim
  48.        
  49.         public static void main(String[] args) {
  50.                
  51.                 int pmax=1000;
  52.                 int m;
  53.                 int i=0;
  54.                 int[] p = new int[pmax];
  55.                 int n;
  56.                
  57.                 p[i]=2; i++;
  58.                 for(m=3; m<pmax; m=m+2) {
  59.                         if (testprim(m)==1) {
  60.                                 p[i]=m;
  61.                                 i++;
  62.                         } // end of if
  63.                        
  64.                 } // end of for-loop
  65.                
  66.                 n=eingabegeradezahl();
  67.                 for (m=0; m<i; m++) goldbachzerlegung(p[m], n);
  68.         } // end of main
  69. } // end of class klausur
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement