Advertisement
Bujkoffer

Gelber Tobias

Mar 3rd, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.70 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class Suchmaschine {
  4.  
  5.     public static boolean istPrimzahl;
  6.  
  7.     public static int[] Liste;
  8.     public static int[] neueArrays;
  9.  
  10.     public static int[] Aufgabe1(int[] a) {
  11.  
  12.         a = new int[1000];
  13.         int oberGrenze = 1000;
  14.  
  15.         for (int i = 0; i < a.length; i++) {
  16.             a[i] = (int) (Math.random() * oberGrenze);
  17.         }
  18.         return a;
  19.     }
  20.  
  21.     public static void main(String[] args) {
  22.  
  23.         Liste = new int[1000];
  24.  
  25.         Liste = Aufgabe1(Liste);
  26.         for (int i = 0; i < Liste.length; i++) {
  27.  
  28.             System.out.println(Liste[i]);
  29.         }
  30.  
  31.         Aufgabe2(Liste);
  32.  
  33.         Aufgabe3(Liste);
  34.  
  35.         Aufgabe4(Liste);
  36.  
  37.         Aufgabe5(Liste);
  38.  
  39.     }
  40.  
  41.     public static void Aufgabe2(int[] a) {
  42.  
  43.         int primzahlCount = 0;
  44.         for (int i = 0; i < a.length; i++) {
  45.             if (istPrimzahl(a[i]))
  46.                 primzahlCount++;
  47.         }
  48.  
  49.         System.out.println(primzahlCount + " von " + a.length + " Zahlen sind Primzahlen");
  50.     }
  51.  
  52.     public static void Aufgabe3(int[] a) {
  53.  
  54.         int letztezahl = 0;
  55.         neueArrays = new int[1000];
  56.         int primzahlCount = 0;
  57.         Arrays.sort(a);
  58.         for (int i = 0; i < a.length; i++) {
  59.             if (istPrimzahl(a[i])) {
  60.                 if (a[i] > letztezahl) {
  61.                     primzahlCount++;
  62.                     System.out.println(primzahlCount + " von " + a[i]);
  63.                     letztezahl = a[i];
  64.                 }
  65.  
  66.             }
  67.         }
  68.         neueArrays = new int[primzahlCount];
  69.         int y = 0;
  70.         letztezahl = 0;
  71.         for (int i = 0; i < a.length; i++) {
  72.             if (istPrimzahl(a[i])) {
  73.                 if (a[i] > letztezahl) {
  74.                     System.out.println(a[i]);
  75.                     neueArrays[y] = a[i];
  76.                     letztezahl = a[i];
  77.                     // System.out.println(primzahlCount + " von " +
  78.                     // neueArrays[y]);
  79.                     y++;
  80.                 }
  81.  
  82.             }
  83.  
  84.         }
  85.         System.out.println(primzahlCount + " von " + y);
  86.     }
  87.  
  88.     public static void Aufgabe4(int[] a) {
  89.         int Anteil = 0;
  90.  
  91.         for (int i = 0; i < a.length; i++) {
  92.  
  93.             if (istGerade(a[i])) {
  94.                 Anteil++;
  95.             }
  96.         }
  97.         System.out.println("Der Anteil gerader Zahlen: " + Anteil);
  98.  
  99.     }
  100.  
  101.     public static void Aufgabe5(int[] a) {
  102.  
  103.         int groessteZahl = 0;
  104.         for (int i = 0; i < a.length; i++) {
  105.             if (a[i] >= groessteZahl)
  106.                 groessteZahl = a[i];
  107.         }
  108.         int kleinsteZahl = groessteZahl;
  109.         for (int i = 0; i < a.length; i++) {
  110.             if (a[i] < kleinsteZahl)
  111.                 kleinsteZahl = a[i];
  112.         }
  113.         System.out.println("Die größte Zahl ist : " + groessteZahl);
  114.         System.out.println("Die kleinste Zahl ist : " + kleinsteZahl);
  115.     }
  116.  
  117.     private static boolean istPrimzahl(int zahl) {
  118.  
  119.         if (zahl <= 1)
  120.             return false;
  121.  
  122.         if (zahl % 2 == 0)
  123.             return false;
  124.  
  125.         int i = 3;
  126.         while (i * i < zahl & zahl % i != 0)
  127.             i = i + 2;
  128.  
  129.         return i * i > zahl;
  130.     }
  131.  
  132.     private static boolean istGerade(int zahl) {
  133.         if (zahl <= 1)
  134.             return false;
  135.  
  136.         if (zahl % 2 == 0)
  137.             return true;
  138.  
  139.         else
  140.             return false;
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement