types:arrays:eratosthenes:loesung2
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
types:arrays:eratosthenes:loesung2 [2022/03/21 07:30] – angelegt Martin Pabst | types:arrays:eratosthenes:loesung2 [2022/03/21 07:34] (aktuell) – Martin Pabst | ||
---|---|---|---|
Zeile 10: | Zeile 10: | ||
<script type=" | <script type=" | ||
- | int anzahl = 100000; | + | int anzahl = Input.readInt(" |
boolean[] gestrichen = new boolean[anzahl + 1]; | boolean[] gestrichen = new boolean[anzahl + 1]; | ||
- | gestrichen[0] | + | // ... sind am Anfang alle mit false initialisiert! |
+ | // gestrichen[0] | ||
+ | |||
+ | // 1 streichen: | ||
gestrichen[1] = true; | gestrichen[1] = true; | ||
- | for(int i = 2; i <= anzahl; i++) { | + | int p = 2; |
- | | + | while(p <= Math.sqrt(anzahl)) { |
- | } | + | // |
- | + | | |
- | int nächstePrimzahl | + | gestrichen[zuStreichen] = true; |
- | while(nächstePrimzahl | + | |
- | // | + | |
- | | + | |
- | | + | |
- | gestrichen[i] = true; | + | |
} | } | ||
- | // nächste | + | // nächste |
- | nächstePrimzahl++; | + | p++; |
- | | + | |
- | | + | |
} | } | ||
+ | |||
} | } | ||
- | // Ausgabe | + | // Ausgabe: |
- | int spalte = 0; | + | for(int i = 1; i <= anzahl; i++) { |
- | for(int i = 2; i <= anzahl; i++) { | + | |
| | ||
- | | + | |
- | spalte++; | + | |
- | if(spalte == 5) { // Zeilenumbruch nach jeweils 5 Spalten | + | |
- | | + | |
- | | + | |
- | } | + | |
} | } | ||
} | } | ||
+ | |||
+ | // Ausgabe der Primzahlzwillinge: | ||
+ | for(int i = 3; i <= anzahl - 2; i++) { | ||
+ | | ||
+ | println(" | ||
+ | } | ||
+ | } | ||
+ | |||
</ | </ | ||
types/arrays/eratosthenes/loesung2.txt · Zuletzt geändert: 2022/03/21 07:34 von Martin Pabst