Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Prime

By: a guest on Oct 30th, 2010  |  syntax: Java  |  size: 0.52 KB  |  views: 195  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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. }