So I was trying to implement Segmented Sieve of Eratosthenes in Java and this is the best I could do. When I run it, it gives no errors but it doesn't return anything, despite me added "System.out.println" at the end to print out all the remaining primes. Thanks in advance for the recommendations
public class SegmentedSieveofEratosthenes {
public static void main(String[] args) throws java.lang.Exception {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
boolean[] v = new boolean[1000000];
int[] primes = new int[1000000];
//counter for the primes vector
int counter = 0;
for(int i=2;i<=(int)Math.sqrt(m);i++)
{
v[i]=true;
}
for(int i=2;i<=(int)Math.sqrt(m);i++)
{
if(v[i]=true)
{
primes[counter]=i;
counter=counter+1;
for(int j=i+1;j<=(int)Math.sqrt(m);j++)
{
if(j%i==0)
{
v[j]=false;
}
}
}
}
boolean[] flags = new boolean[1000000];
for(int i=n;i<=m;i++)
{
flags[i]=true;
}
for(int i=0;i<counter;i++)
{
int start = (n/primes[i])*3;
for(int j=start+i;j<=m;j=j+i)
flags[j]=false;
}
for(int i=n;i<=m;i++)
if(flags[i]==true)
System.out.println(i);
in.close();
}
}