java - Sieve of Atkins, unexpected results -


so working on sieve of atkins problem 10.

question:

"the sum of primes below 10 2 + 3 + 5 + 7 = 17.

find sum of primes below 2 million."

i believe following steps correctly it's failing give right answer.

import java.util.arraylist; import java.util.hashmap; import java.util.map; import java.util.map.entry;   public class level_10 {      public static void main(string[] args) {         //sieve of atkins         int limit = 2000000;         long sum = 0;         int ins = 2;         int limitsq = ((int)math.round(math.sqrt(limit)));         arraylist<long> results = new arraylist<long>();         //2, 3. , 5 primes         results.add(2l);         results.add(3l);         results.add(5l);         map<long, boolean> sieves = new hashmap<long, boolean>(); //composite list         //adding sieves         (int = 5; <= limit; i++) sieves.put((long) i,false);      long n;     (int x = 1; x < limitsq; x++) {         (int y = 1; y < limitsq; y++) {             //4x^2 + y^2             n = ((4*(x*x)) + (y*y));             if (n <= limit && (n % 12 == 1 || n % 12 == 5)) sieves.put(n*n, true);             //3x^2 + y^2             n = ((3*(x*x)) + (y*y));             if (n <= limit && (n % 12 == 7)) sieves.put(n*n, true);             //3x^2 − y^2             n = ((3*(x*x)) - (y*y));             if (x > y && n <= limit && (n % 12 == 11)) sieves.put(n*n, true);          }     }      //multiple of squares not prime     (int num = 1; num < limitsq; num++) {         if (sieves.get(num) != null) (int m = num * num; m <= limit; m += num * num) sieves.put((long) m, false);     }      //add results          (entry<long, boolean> nextsieve : sieves.entryset()) {         if (nextsieve.getvalue() && ins % 2 == 0) {             results.add(nextsieve.getkey());             ins++;         }     }     //generate sum     (long resultnum : results) sum += resultnum;      system.out.println("the sum of primes below " + limit + " " + sum);       } } 

if here inefficient, please notify me.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -