code_junkie

Using a long as ArrayList index in java

Nov 13th, 2011
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. import java.util.*;
  2. import java.lang.*;
  3.  
  4. public class t3{
  5. public static void main(String[] args){
  6. long num = 100;
  7.  
  8. //declaring list and filling it with numbers
  9. ArrayList<Long> numlist = new ArrayList<Long>();
  10. for(long x=2 ; x<num ; x++){
  11. numlist.add(new Long(x));
  12. }
  13.  
  14. //sieve or eratosthenes
  15. for(long x=0 ; x<Math.sqrt(num) ; x++){
  16. for(long y=x+1 ; y<numlist.size() ; y++){
  17. if(numlist[y]%numlist[x] == 0){
  18. numlist.remove(y);
  19. }
  20. }
  21. }
  22.  
  23. //print list
  24. for(Object item : numlist){
  25. System.out.println((Long)item);
  26. }
  27. }
  28. }
Add Comment
Please, Sign In to add comment