Sieve of Eratosthenes java

0

Hej,
Jak w temacie chcę zrobić sito. Trochę się zamotałem. Potrzebuje podpowiedzi.

class SieveOfEratosthenes {

private void sieveOfEratosthenes(int n) {
    int prime[] = new int[n + 1];
    for (int i = 0; i < n; i++) {
        prime[i] = i;
    }
    for (int first = 2; first * first <= n; first++) {
        if (prime[first] > 0) {
            for (int i = first * 2; i <= n; i += first)
                prime[i] = 0;
        }
    }
    int numberPrimes = 0;
    for (int i = 2; i <= n; i++) {
        if (prime[i] > 0) {
            System.out.print(i + " ");
            numberPrimes++;
        }
    }
    int[] primes = new int[numberPrimes + 1];
    for (int i = 0; i < n; i++) {
        primes[i] =        -------------------------------------------------------->   potrzebuje wypełnić tablicę wyników  elementami oryginalnej tablicy, które nie są zerami :) i zwrócić tablicę.
        return primes; 

Co zrobiłem: zadeklarowałem tablice, wypełniłem liczbami(najpierw zerami), usunąłem mnożniki, napisałem pętle na główne liczby, zadeklarowałem tablicę dla wyniku rozmiaru obliczonego w poprzednim kroku.
}
}

public static void main(String args[]) {
    int n = 120;
    System.out.print("This is the range of numbers " + n);
    System.out.println(" ");
    System.out.println("Prime numbers in the range: ");
    SieveOfEratosthenes sieve = new SieveOfEratosthenes();
    sieve.sieveOfEratosthenes(n);
0

Dobra już wiem co i jak, brakowało mi ;p
int[] primes = new int[numberPrimes];
int j = 0;
for (int i = 2; i <= n; i++) {
if (prime[i] > 0) {
// System.out.print(i + " ");
primes[j++] = i;
}
}
return primes;
}

zamykam

1 użytkowników online, w tym zalogowanych: 0, gości: 1