Advertisement
Guest User

Prime

a guest
Oct 30th, 2010
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.52 KB | None | 0 0
  1. public class BitSetPrime {
  2.  
  3.     public static void main(String[] args) {
  4.     BitSet p=new BitSet(2000000);
  5.     p.flip(2,2000000); //Considering all numbers to be prime except 0 and 1
  6.     for(int i=2 ;i<2000000;i++)
  7.     {
  8.         if(!p.get(i))
  9.             continue;
  10.         else
  11.               // Set all multiples as not prime
  12.               for(int j = 2*i; j < 2000000; j += i)
  13.                  p.set(j,false);
  14.     }
  15.     long sum=0;
  16.      for(int i = 2; i < 2000000; ++i)
  17.             if( p.get(i) )  // Add all nos with bit set
  18.               sum += i;
  19.     System.out.println(sum);
  20.    
  21. }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement