public static ArrayList<Integer> generate_primes_from_1_to_n(int n) {
        int size = (int)Math.floor(0.5 * (n - 3)) + 1;
        // is_prime[i] represents (2i + 3) is prime or not.
        ArrayList<Integer> primes = new ArrayList<Integer>();  // stores the primes from 1 to n.
        primes.add(2);
        boolean is_prime[] = new boolean[size];
        Arrays.fill(is_prime, true);
        for (long i = 0; i < size; ++i) {
            if (is_prime[(int) i]) {
                int p = (int) ((i << 1) + 3);
                primes.add(p);
                // Sieving from p^2, whose index is 2i^2 + 6i + 3.
                for (long j = ((i * i) << 1) + 6 * i + 3; j < size; j += p) {
                    is_prime[(int) j] = false;
                }
            }
        }
        return primes;
    }