what are the mistakes in my code:
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int t = cin.nextInt();
while(t>0){
int count = 0;
ArrayList alist = new ArrayList();
long a = cin.nextLong(),b = cin.nextLong();
for(long i = a;i<=b;i++){
for(long j =a;j<=i;j++){
if(i%j == 0){
count ++;
}
}
if(count == 2){
alist.add(i);
}
}
System.out.println(alist.size());
t–;
}
}
}
Prime visits problem doubt
- You don’t need to take ‘long’, just take ‘int’
here ‘j’ should be initialized with 1, not a.
3)
This should be initialized as 'ArrayList<Integer> alist = new ArrayList<>();
4) You will have to make your algorithm more efficient otherwise all test cases will not pass. For example, you could run the second loop till sqrt(i) or use Sieve of Eratosthenes
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.