Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class primzahl {
- public static void main(String[] args) {
- int max = 100;
- boolean[] isPrim = new boolean[max + 1];
- // Initialierung des Arrays
- for (int i = 0; i <= max; i++)
- isPrim[i] = true;
- // 0 und 1 sind keine Primzahlen
- isPrim[0] = isPrim[1] = false;
- // alle Vielfachen von Ganzzahlen ausschließen,
- // die kleiner als die Quadratwurzel von max sind.
- int n = (int) Math.ceil(Math.sqrt(max));
- for (int i = 0; i <= n; i++)
- if (isPrim[i])
- for (int j = 2 * i; j <= max; j+=i)
- isPrim[j] = false;
- // Primzahlen ausgeben
- System.out.print("Primzahlen anzeigen von 0 bis " + max + ": ");
- for (int i = 0; i <= max; i++)
- if (isPrim[i])
- System.out.print(" " + i + " ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement