51 lines
1.4 KiB
Java
51 lines
1.4 KiB
Java
package task5;
|
|
|
|
public class PrimeNumberGeneratorAI {
|
|
private final int maximum;
|
|
|
|
public PrimeNumberGeneratorAI(int maximum) {
|
|
this.maximum = maximum;
|
|
}
|
|
|
|
public void printPrimeNumbers() {
|
|
for (int i = 2; i <= maximum; i++) {
|
|
if (isPrime(i)) {
|
|
System.out.println(i);
|
|
}
|
|
}
|
|
}
|
|
|
|
private boolean isPrime(int number) {
|
|
if (number < 2) return false;
|
|
if (number == 2) return true;
|
|
if (number % 2 == 0) return false;
|
|
for (int i = 3; i * i <= number; i += 2) {
|
|
if (number % i == 0) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public int countPrimes() {
|
|
int count = 0;
|
|
for (int i = 2; i <= maximum; i++) {
|
|
if (isPrime(i)) {
|
|
count++;
|
|
}
|
|
}
|
|
return count;
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
int[] testValues = {100, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000, 200000};
|
|
|
|
System.out.println("maximum,Anzahl Primzahlen (Algorithmus),maximum/ln(maximum) (Schätzung)");
|
|
for (int max : testValues) {
|
|
PrimeNumberGeneratorAI generator = new PrimeNumberGeneratorAI(max);
|
|
int counted = generator.countPrimes();
|
|
double estimated = max / Math.log(max);
|
|
System.out.printf("%d,%d,%.1f%n", max, counted, estimated);
|
|
}
|
|
}
|
|
} |