Advertisement
Guest User

Untitled

a guest
May 13th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.93 KB | None | 0 0
  1. public class Sieve {
  2.  
  3.     public static void main(String[] args) {
  4. //         assume here is a check of command line arg
  5.         long bound = 10_000_000;
  6.         Set<Long> primes = new LinkedHashSet<>();
  7.         long startTime = System.currentTimeMillis();
  8.         for (long start = 3; start <= bound; start += 2) {
  9.  
  10.             long root = (long) Math.sqrt(start);
  11.  
  12.             boolean isPrime = true;
  13.             for (Long l : primes) {
  14.                 if (l > root) break;
  15.                 if (start % l == 0) {
  16.                     isPrime = false;
  17.                     break;
  18.                 }
  19.             }
  20.             if (isPrime)
  21.                 primes.add(start);
  22.  
  23.         }
  24.  
  25.         primes.add(2L);
  26.  
  27.         System.out.println(primes);
  28.         System.out.println("Duration: " + (System.currentTimeMillis() - startTime) / 1000.0 + " seconds");
  29.         System.out.println("Primes found: " + primes.size());
  30.     }
  31.  
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement