Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. public static void main( String[] args )
  2. {
  3. int limit = 2000000;
  4. boolean[] primes = new boolean[( limit + 1 )];
  5. primes[0] = false;
  6. primes[1] = false;
  7. for ( int x = 2; x < ( limit + 1 ); x++ )
  8. {
  9. primes[x] = true;
  10. }
  11. long sum = 0;
  12. int x = 3;
  13.  
  14. for ( int index = 2; ( index * index ) <= limit; index++ )
  15. {
  16. while ( !primes[index] )
  17. {
  18. index++;
  19. }
  20.  
  21. x = index * ( index - 1 );
  22. if ( x <= limit - index )
  23. {
  24. x = x + index;
  25. primes[x] = false;
  26. System.out.println( "marking " + x + " as false" );
  27. }
  28.  
  29. }
  30.  
  31. for ( int index = 2; index < limit; index++ )
  32. {
  33. while ( !primes[index] )
  34. {
  35. index++;
  36. if ( index == limit )
  37. {
  38. break;
  39. }
  40. }
  41. sum = sum + index;
  42. System.out.println( "adding " + index + '\t' + "current sum is "
  43. + sum );
  44. }
  45. sum = sum - limit;
  46. System.out.println("subtracting the limit");
  47. System.out.println( "The sum is " + sum );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement