static List<Integer> genPrimes(int n) {
    List<Integer> primes = new ArrayList<Integer>();
    
    boolean[] sieve = new boolean[n + 1];
    for (int x = 0; x < n + 1; x++) {
      sieve[x] = true;
    }
    for (int x = 2; x <= n; x++) {
      if (sieve[x]) {
        primes.add(x);
        int val = 2 * x;
        while (val <= n) {
          sieve[val] = false;
          val += x;
        }
      }
    }
    
    return primes;
  }