Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- public class primzahl {
- public static String tmp;
- public static int i;
- public static void main(String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- try {
- System.out.println("Bitte geben sie die Maximale länge der zu berechnenden Zahlen ein.");
- tmp = br.readLine();
- } catch (IOException io) {
- System.out.println("Error beim einlesen der Tastatureingaben");
- }
- int max = 0;
- max = Integer.parseInt(tmp);
- 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 (i = 0; i <= max; i++)
- if (isPrim[i])
- System.out.print(" " + i + " ");
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement