Time limit exceeding in test case 3

my code is working fine but time limit is exceeding in 3rd test case.please look at my code and suggest some necessary changes.

Hi @akshay9009
share your code here.

import java.util.*; public class Main { public static void main(String args[]) { Scanner scan=new Scanner(System.in); int n=scan.nextInt(); for(int j=1;j<=n;j++){ int a=scan.nextInt(); int b=scan.nextInt(); int count=0; for(int i=a;i<=b;i++){ if(isprime(i)){ count++; } } System.out.print(count); System.out.println(""); } } public static boolean isprime(int n){ if(n<=1){ return false; } for(int i=2;i<=Math.sqrt(n);i++){ if(n%i==0){ return false; } } return true; } }

Hi @akshay9009,use seive method to find the prime numbers.

Hi,
As you are not responding to the thread, I am marking your doubt as resolved for now. Re-open it if required.
Please mark your doubts as resolved in your course’s “Ask Doubt” section, when your doubt is resolved.

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.