Add Task 5 prime number generator, AI comparison, and .gitignore
This commit is contained in:
45
aufgabenblatt1/src/task5/PrimeNumberGenerator.java
Normal file
45
aufgabenblatt1/src/task5/PrimeNumberGenerator.java
Normal file
@@ -0,0 +1,45 @@
|
||||
package task5;
|
||||
|
||||
public class PrimeNumberGenerator {
|
||||
private int maximum;
|
||||
|
||||
public PrimeNumberGenerator(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) {
|
||||
for (int i = 2; i < number; i++) {
|
||||
if (number % i == 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
private 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) {
|
||||
PrimeNumberGenerator generator = new PrimeNumberGenerator(max);
|
||||
int counted = generator.countPrimes();
|
||||
double estimated = max / Math.log(max);
|
||||
System.out.printf("%d,%d,%.1f%n", max, counted, estimated);
|
||||
}
|
||||
}
|
||||
}
|
||||
51
aufgabenblatt1/src/task5/PrimeNumberGeneratorAI.java
Normal file
51
aufgabenblatt1/src/task5/PrimeNumberGeneratorAI.java
Normal file
@@ -0,0 +1,51 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
aufgabenblatt1/src/task5/primzahlen_ergebnisse.csv
Normal file
11
aufgabenblatt1/src/task5/primzahlen_ergebnisse.csv
Normal file
@@ -0,0 +1,11 @@
|
||||
maximum,Anzahl Primzahlen (Algorithmus),maximum/ln(maximum) (Schätzung)
|
||||
100,25,21.7
|
||||
500,95,80.5
|
||||
1000,168,144.8
|
||||
2000,303,263.1
|
||||
5000,669,587.0
|
||||
10000,1229,1085.7
|
||||
20000,2262,2019.5
|
||||
50000,5133,4621.2
|
||||
100000,9592,8685.9
|
||||
200000,17984,16385.3
|
||||
|
Reference in New Issue
Block a user