Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.79 KB | None | 0 0
  1.  // Fills the array with the first array.length prime numbers;
  2.     // assumes that each array entry at an index below n is already correctly set.
  3.     private static void primes(int[] array, int n) {
  4.         if (n < array.length) {
  5.             if (n == 0) {
  6.                 array[0] = 2;
  7.                 primes((array), (n+1));
  8.             } else {
  9.                 int check = array[n - 1];
  10.                 boolean isPrime;
  11.                 do {
  12.                     check++;
  13.                     isPrime = true;
  14.                     for (int i = 0; i < n; i++) {
  15.                         isPrime = isPrime && (check % array[i] != 0);
  16.                     }
  17.                 } while (! isPrime);
  18.                 array[n] = check;
  19.                 primes((array), (n+1));
  20.             }
  21.         }
  22.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement