Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //01151424 David Karasz
- public class Bsp06 {
- public static void main(String[] args) {
- System.out.println("Alle Primzahlen in [m, n]");
- System.out.print("m: ");
- int m = SavitchIn.readInt();
- System.out.println();
- System.out.print("n: ");
- int n = SavitchIn.readInt();
- System.out.println();
- System.out.println(String.format("Alle Primzahlen in [%d, %d]:", m, n));
- boolean[] primes = findPrimes(m, n);
- int primesCount = printPrimes(primes, m);
- System.out.println("Insgesamt " + primesCount + " Primzahlen.");
- }
- public static boolean isPrime(int n) {
- //ueberprueft ob n eine Primzahl ist
- int k = 2;
- while((k <= n) && (n%k != 0) && (n > 1))
- k++;
- return k == n;
- }
- public static boolean[] findPrimes(int m, int n) {
- if (n < m) return null;
- int size = n - m + 1;
- boolean[] primes = new boolean[size];
- for (int i = m; i <= n; i++) {
- primes[i - m] = isPrime(i);
- }
- return primes;
- }
- public static int printPrimes(boolean[] foundPrimes, int m) {
- int primes = 0;
- for (int i = 0; i < foundPrimes.length; i++) {
- if (foundPrimes[i]) {
- System.out.print(m + i + " ");
- primes++;
- }
- }
- System.out.println();
- return primes;
- }
- public static boolean[] sieveOfEratosthenes(int m, int n) {
- return findPrimes(m, n);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement