17.4 Sieb des Erathostenes (Das Programm)
 
Der Algorithmus Der Algorithmus: Sieb des Erathostenes ist uns schon früher begegnet. Wenn man sich nicht mehr genau erinnert, schaue man dort noch einmal nach.
Algorithmus: Sieb des Erathostenes

 
Download imoort info1.*;
public class Erathostenes {
  public static void main(String[] args) {
    System.out.print("Bis zu welcher Zahl soll auf prim
                      geprueft werden? "
);
    int max = Console.in.readInt();
    boolean[] primzahlen;
    primzahlen = new boolean[max];
    //Initialisierungsschleife
    for (int n = 2; n < primzahlen.length; n++) {
      primzahlen[n] = true;
    }
    //Sieb

    for (int n = 2; n < Math.sqrt(primzahlen.length);n++) {
     
if(primzahlen[n]) {
        for (int i = 2*n; i < primzahlen.length; i += n) {
          primzahlen[i] = false;
        }
      }
    }
   //Ausgabe

    for ( int n = 2; n < primzahlen.length; n++) { 
     
if (primzahlen[n]) {
         System.out.println(n);
      }
    }
  }
}
zu 17.5 Übungen
zur Startseite www.pohlig.de  (C) MPohlig 2004