Could you give a few more details such as the limits of x and y. The first approach that comes to my head is to use sieve of Eratosthenes. Only difference is that instead of eliminating multiples of prime numbers we eliminate multiples of perfect squares. If x and y are very large but y-x is of acceptable size (around 10^5 -10^6) then you can use a segmented sieve like approach for this.