Guest User

Untitled

a guest
Dec 11th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class Eratosthene{
  4.  
  5. public static int[] byteEratosthenes(int cap) {
  6. int[] result = new int[(int) Math.ceil(1.25 * cap / Math.log(cap))];
  7. int pos = 1;
  8. result[0] = 2;
  9. byte[] primes = new byte[(cap - 2 >> 4) + 1];
  10.  
  11. for (int i = 0; i < cap - 2 >> 1; i++) {
  12. if ((primes[i >> 3] >> (i & 7) & 1) == 0) { // mod => 1 & n^2 for 8086
  13. result[pos++] = (i * 2 + 3);
  14. for (long j = 4L * i * i + 12L * i + 9L; j < cap; j += i * 4 + 6) {
  15. int number = (int) (j - 3L >> 1);
  16. byte mask = (byte) (0b1 << (number & 7));
  17. primes[(number >> 3)] |= mask;
  18. }
  19. }
  20. }
  21. if (result.length > pos) {
  22. result = Arrays.copyOf(result, pos);
  23. }
  24. return result;
  25. }
  26. }
Add Comment
Please, Sign In to add comment