View difference between Paste ID: z2u3RqWS and
SHOW:
|
|
- or go back to the newest paste.
1 | - | |
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 | } |