ellesehc

Sum of primes below 2 million(a little bit faster)

Nov 12th, 2015
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. public class Trial1
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.             final long startTime = System.currentTimeMillis();
  6.                         int count = 0;
  7.                         long result = 5;
  8.                        
  9.                         for(int x = 5; x < 2_000_000; x++)
  10.                         {                          
  11.                             if(x % 2 != 0)
  12.                             {
  13.                                 if(x % 3 != 0)
  14.                                 {
  15.                                 for(int y = 5; y <= Math.round(x / 5); y += 2)
  16.                                         {  
  17.                                                 if(x % y == 0)
  18.                                                 {
  19.                                                         count += 1;
  20.                                                 }
  21.                                         }
  22.                                
  23.                                         if(count == 0)
  24.                                         {
  25.                                                 result += x;
  26.                                         }
  27.                                        
  28.                                         else
  29.                                         {
  30.                                                 count = 0;
  31.                                         }
  32.                                 }                    
  33.                             }
  34.                         }
  35.                        
  36.                         System.out.println("Result: " + result );
  37.                         final long endTime = System.currentTimeMillis();
  38.                         final double total = (double)(endTime - startTime) / (double)1000;
  39.                         System.out.println("Total execution time: " + total +" seconds");
  40.         }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment