Advertisement
Bujkoffer

Arraysuchmaschine

Feb 23rd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.99 KB | None | 0 0
  1. public class Arrays {
  2.  
  3.     public static boolean istPrimzahl;
  4.  
  5.     public static int[] Liste;
  6.  
  7.     public static int[] Aufgabe1(int[] a) {
  8.  
  9.         a = new int[1000];
  10.         int oberGrenze = 1000;
  11.  
  12.         for (int i = 0; i < a.length; i++) {
  13.             a[i] = (int) (Math.random() * oberGrenze);
  14.         }
  15.         return a;
  16.     }
  17.  
  18.     public static void main(String[] args) {
  19.  
  20.         Liste = new int[1000];
  21.  
  22.         Liste = Aufgabe1(Liste);
  23.         for (int i = 0; i < Liste.length; i++) {
  24.  
  25.             System.out.println(Liste[i]);
  26.         }
  27.  
  28.         Aufgabe2(Liste);
  29.  
  30.     }
  31.  
  32.     public static void Aufgabe2(int[] a) {
  33.  
  34.         int primzahlCount = 0;
  35.         for (int i = 0; i < a.length; i++) {
  36.             if (istPrimzahl(a[i]))
  37.                 primzahlCount++;
  38.         }
  39.  
  40.         System.out.println(primzahlCount + " von " + a.length
  41.                 + " Zahlen sind Primzahlen");
  42.     }
  43.  
  44.     private static boolean istPrimzahl(int zahl) {
  45.         if (zahl <= 1)
  46.             return false;
  47.  
  48.         if (zahl % 2 == 0)
  49.             return false;
  50.  
  51.         int i = 3;
  52.         while (i * i < zahl & zahl % i != 0)
  53.             i = i + 2;
  54.  
  55.         return i * i > zahl;
  56.     }
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement