Advertisement
Bujkoffer

Ick und mein jelbet T-Shirt

Mar 3rd, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 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.                     neueArrays[y] = a[i];
  75.                     letztezahl = a[i];
  76.  
  77.                     System.out.println(primzahlCount + " von " + neueArrays[y]);
  78.                     y++;
  79.                 }
  80.  
  81.             }
  82.         }
  83.  
  84.     }
  85.  
  86.     public static void Aufgabe4(int[] a) {
  87.         int Anteil = 0;
  88.  
  89.         for (int i = 0; i < a.length; i++) {
  90.  
  91.             if (istGerade(a[i])) {
  92.                 Anteil++;
  93.             }
  94.         }
  95.         System.out.println("Der Anteil gerader Zahlen: " + Anteil);
  96.  
  97.     }
  98.  
  99.     public static void Aufgabe5(int[] a) {
  100.  
  101.         int groessteZahl = 0;
  102.         for (int i = 0; i < a.length; i++) {
  103.             if (a[i] >= groessteZahl)
  104.                 groessteZahl = a[i];
  105.         }
  106.         int kleinsteZahl = groessteZahl;
  107.         for (int i = 0; i < a.length; i++) {
  108.             if (a[i] < kleinsteZahl)
  109.                 kleinsteZahl = a[i];
  110.         }
  111.         System.out.println("Die größte Zahl ist : " + groessteZahl);
  112.         System.out.println("Die kleinste Zahl ist : " + kleinsteZahl);
  113.     }
  114.  
  115.     private static boolean istPrimzahl(int zahl) {
  116.  
  117.         if (zahl <= 1)
  118.             return false;
  119.  
  120.         if (zahl % 2 == 0)
  121.             return false;
  122.  
  123.         int i = 3;
  124.         while (i * i < zahl & zahl % i != 0)
  125.             i = i + 2;
  126.  
  127.         return i * i > zahl;
  128.     }
  129.  
  130.     private static boolean istGerade(int zahl) {
  131.         if (zahl <= 1)
  132.             return false;
  133.  
  134.         if (zahl % 2 == 0)
  135.             return true;
  136.  
  137.         else
  138.             return false;
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement