Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Sieb {
- static int n = 1000000;
- static boolean sieb[] = new boolean[n];
- static int counter = 0;
- static String primzahlen = "";
- public static void main(String[] args) {
- // Schritt 0: Initialisiere alles mit false
- for(int i=0; i<n; i++) {
- sieb[i] = false;
- }
- // Schrit 1: Markiere die Zahl 1
- // Anmerkung: Array Indizes starten bei 0
- sieb[0] = true;
- counter++;
- // Schritt 2: Counter zeigt aktuell auf die erste nicht eingerahmte Primzahl
- // Anmerkung: Bei uns passiert hier nichts
- while(counter*counter <= n) {
- //Schritt 3: Streiche alle Vielfache durch
- // i+=counter+1 === i = i + (counter + 1)
- for(int i=2*(counter+1)-1; i<n; i+=counter+1) {
- //System.out.println(i+1);
- sieb[i] = true;
- }
- // Gehe zur nächsten Zahl
- counter++;
- // Überspringe alle Zahlen, die true sind, weil diese bereits markiert worden sind
- // while(sieb[counter] == true) {
- while(sieb[counter]) {
- counter++;
- }
- }
- // Gib alle unmarkierten Zahlen aus
- for(int i=0; i<n; i++) {
- //if(sieb[i] == false) {
- if(!sieb[i]) {
- //System.out.println(i+1);
- primzahlen += Integer.toString(i+1) + ", ";
- }
- }
- primzahlen = primzahlen.substring(0, primzahlen.length() - 2);
- System.out.println(primzahlen);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement