Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class Suchmaschine {
- public static boolean istPrimzahl;
- public static int[] Liste;
- public static int[] neueArrays;
- public static int[] Aufgabe1(int[] a) {
- a = new int[1000];
- int oberGrenze = 1000;
- for (int i = 0; i < a.length; i++) {
- a[i] = (int) (Math.random() * oberGrenze);
- }
- return a;
- }
- public static void main(String[] args) {
- Liste = new int[1000];
- Liste = Aufgabe1(Liste);
- for (int i = 0; i < Liste.length; i++) {
- System.out.println(Liste[i]);
- }
- Aufgabe2(Liste);
- Aufgabe3(Liste);
- Aufgabe4(Liste);
- Aufgabe5(Liste);
- }
- public static void Aufgabe2(int[] a) {
- int primzahlCount = 0;
- for (int i = 0; i < a.length; i++) {
- if (istPrimzahl(a[i]))
- primzahlCount++;
- }
- System.out.println(primzahlCount + " von " + a.length + " Zahlen sind Primzahlen");
- }
- public static void Aufgabe3(int[] a) {
- int letztezahl = 0;
- neueArrays = new int[1000];
- int primzahlCount = 0;
- Arrays.sort(a);
- for (int i = 0; i < a.length; i++) {
- if (istPrimzahl(a[i])) {
- if (a[i] > letztezahl) {
- primzahlCount++;
- System.out.println(primzahlCount + " von " + a[i]);
- letztezahl = a[i];
- }
- }
- }
- neueArrays = new int[primzahlCount];
- int y = 0;
- letztezahl = 0;
- for (int i = 0; i < a.length; i++) {
- if (istPrimzahl(a[i])) {
- if (a[i] > letztezahl) {
- neueArrays[y] = a[i];
- letztezahl = a[i];
- System.out.println(primzahlCount + " von " + neueArrays[y]);
- y++;
- }
- }
- }
- }
- public static void Aufgabe4(int[] a) {
- int Anteil = 0;
- for (int i = 0; i < a.length; i++) {
- if (istGerade(a[i])) {
- Anteil++;
- }
- }
- System.out.println("Der Anteil gerader Zahlen: " + Anteil);
- }
- public static void Aufgabe5(int[] a) {
- int groessteZahl = 0;
- for (int i = 0; i < a.length; i++) {
- if (a[i] >= groessteZahl)
- groessteZahl = a[i];
- }
- int kleinsteZahl = groessteZahl;
- for (int i = 0; i < a.length; i++) {
- if (a[i] < kleinsteZahl)
- kleinsteZahl = a[i];
- }
- System.out.println("Die größte Zahl ist : " + groessteZahl);
- System.out.println("Die kleinste Zahl ist : " + kleinsteZahl);
- }
- private static boolean istPrimzahl(int zahl) {
- if (zahl <= 1)
- return false;
- if (zahl % 2 == 0)
- return false;
- int i = 3;
- while (i * i < zahl & zahl % i != 0)
- i = i + 2;
- return i * i > zahl;
- }
- private static boolean istGerade(int zahl) {
- if (zahl <= 1)
- return false;
- if (zahl % 2 == 0)
- return true;
- else
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement