Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. public static void main(String[] args)
  2. {
  3. boolean[] test = sieve(createSequence(15));
  4. for (int index = 0; index < test.length; index++)
  5. if (test[index] = true)
  6. System.out.print("T");
  7. }
  8. public static boolean[] createSequence(int input)
  9. {
  10. int i = 2;
  11. boolean[] numArray = new boolean[input-1];
  12. for(int index=0; i <input+1; index++)
  13. {
  14. numArray[index] = true;
  15. i++;
  16. }
  17. return numArray;
  18. }
  19. public static boolean[] crossOutHigherMultiples(int i, boolean[] sequence)
  20. {
  21. for (int index = 2; index*i <= sequence.length+1; index++)
  22. sequence[(index*i)-2] = false;
  23. return sequence;
  24. }
  25. public static boolean[] sieve(boolean[] sequence)
  26. {
  27. for (int index = 2; sequence[index] = true && index > Math.sqrt(sequence.length); index++)
  28. {
  29. sequence = crossOutHigherMultiples(index, sequence);
  30. }
  31. return sequence;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement