Prime visits query

Sir/Maam
How do i resolve this problem I have applied two different methods to solve this. Even applying two different methods :
Scanner obj = new Scanner(System.in);

    int[] count = new int[1000000];
    count[0] = 0;
    count[1] = 0;
    count[2] = 1;
    count[3] = 2;
    
	int rem = 0, x = 4;

    for(int j = 4; j<count.length; j++){
        for(int k=2; k<=Math.sqrt(j); k++){
            rem = j%k;
            if(rem==0){
                count[x] = count[x-1];
                x++;
                break;
            }
        }
        if(rem!=0){
            count[x] = count[x-1] + 1;
            x++;
        }
    }
    
    int n = obj.nextInt();
    if(n<=1000){
        for(int i=0; i<n; i++){
            int a = obj.nextInt();
            while(a>1000000){
                a = obj.nextInt();
            }
            int b = obj.nextInt();
            while(b>1000000){
                b = obj.nextInt();
            }
            int max = (a>b ? a : b);
            int min = (a>b ? b : a);
            System.out.println(count[max]-count[min-1]);
        }
    }

And other method is :
{

	Scanner obj = new Scanner(System.in);
	int n = obj.nextInt();

	if(n<=1000){
		for(int i=1; i<=n; i++){
			int a = obj.nextInt();
			int b = obj.nextInt();
			int min, max;

			if(a<=1000000 && b<=1000000){
				if(a<b){
					min=a;
					max=b;
				}else{
					min=b;
					max=a;
				}
				System.out.println(divisor(min,max));
			}
		}
	}
}

public static int divisor(int a, int b){
	int count = 0, m = 0;
	if(a<=2){
		count+=1;
	}
	for(int i=a; i<=b; i++){
		for(int j=2; j<i; j++){
			m=i%j;
			if(m==0){
				break;
			}
		}
		if(m!=0){
			count+=1;
		}
	}
	return count;
}

Hi Ekram,
In your first approach, if min=0 your code will give error and second code is exceeding the Time Limit. Try to use math.sqrt(i) like you have done in your first approach and add some base conditions to it.

Thank you sir. It worked.