PRIME1 time limit exceeded HELP!

getting time limit exceeded for the problem PRIME1 .what should i do

#include<iostream>
#include<stdio.h>

int main()

{

int x,t;
unsigned long a,b,i,j;
scanf("%ul",&t);
while(t--)
{
    scanf("%u",&a);
    scanf("%u",&b);
    for(i=(a==1?2:a);i<=b;i++)
    {
        x=0;
        for(j=2;j<i;j++)
            if(i%j==0)
            {
                x++;
                break;
            }
        if(x==0)
            printf("%u\n",i);
    }
}

}

brute force will not work because numbers are very big. try to optimize your approach

Read about O(sqrt(x)) prime factorisation.

1 Like

Read this topic “Sieve of Eratosthenes”.

//