Skip to content

Instantly share code, notes, and snippets.

@tolpp
Last active December 23, 2017 12:33
Show Gist options
  • Select an option

  • Save tolpp/cef616c3668714091ca2 to your computer and use it in GitHub Desktop.

Select an option

Save tolpp/cef616c3668714091ca2 to your computer and use it in GitHub Desktop.
public class Eratosthenes {
static int[] Sieve(int n) {
int[] A = new int[n + 1];
int[] L = new int[n + 1];
for (int p = 2; p < n; p++) A[p] = p; // ilk veriler atanıyor
for (int p = 2; p < (int) Math.sqrt(n); p++) {
if (A[p] != 0) {
int j = p * p;
while (j < n) { // asal sayının katları eleniyor
A[j] = 0;
j = j + p;
}
}
}
int i = 0;
for (int p = 2; p < n; p++) {
if (A[p] != 0) {
L[i] = A[p];
i++;
}
}
L = Arrays.copyOf(L, i);
return L;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment