1 / 2
Feb 2021

This is my code, but spoj shows me that time limit exceeded. Can anyone suggest me a solution by making changes in my code?

#include <stdio.h>
int prime(int);
int main(void) {
	// your code here
	int t,m,n;
	int i,j;
	scanf("%d", &t);
   	for(i=0;i<t;i++){
    	scanf("%d %d",&m,&n);
    	if(1<= m && m<= n && m<= 1000000000 && n<= 1000000000 && n-m<=100000){
    	  	int f1=prime(m);
    	  	int f2=prime(n);
    	  	if(f1==1){
    	  		printf("\n %d",m);
    	  		for(j=m;j<n;j+=2){
    	  			printf("\n %d",j);
    	  		}
    	  	}
    	  	if(f2==1){
    	  		printf("\n %d",n);
    	  	}
    	}
   	}
	return 0;
}
int prime(int x){
	int y=x;
	int k,c;
	for(k=1;k<=y;k++){
		if(x%k==0){
			c++;
		}
	}
	if(c==2){
		return 1;
	}
	else{
		return 0;
	}
}`Preformatted text`
  • created

    Feb '21
  • last reply

    Feb '21
  • 1

    reply

  • 546

    views

  • 2

    users

change prime(x) function in order to divide number x ONLY by PRIME numbers which are less than or equal to x^0.5