Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Sieve {
- public static void main(String[] args) {
- // assume here is a check of command line arg
- long bound = 10_000_000;
- Set<Long> primes = new LinkedHashSet<>();
- long startTime = System.currentTimeMillis();
- for (long start = 3; start <= bound; start += 2) {
- long root = (long) Math.sqrt(start);
- boolean isPrime = true;
- for (Long l : primes) {
- if (l > root) break;
- if (start % l == 0) {
- isPrime = false;
- break;
- }
- }
- if (isPrime)
- primes.add(start);
- }
- primes.add(2L);
- System.out.println(primes);
- System.out.println("Duration: " + (System.currentTimeMillis() - startTime) / 1000.0 + " seconds");
- System.out.println("Primes found: " + primes.size());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement