1 / 7
Oct 2015
#include <iostream>
#include <cmath>

using namespace std;
unsigned int numbers[3500],len;
inline bool prime(unsigned int x)
{
    unsigned int i,last = sqrt(x);
    for(i=2;i<last;i++)
    {
        if(!(x%i))
        {
            return 0;
        }
    }
    return 1;
}
void generate()
{
    for(unsigned int i=2;i<32000;i++)
    {
        if(prime(i))
        {
            numbers[len++] = i;
        }
    }
}
inline bool process(unsigned int x)
{
    unsigned int i,last = sqrt(x);
    for(i=0;i<last && numbers[i]<=last;i++)
    {
        if(!(x%numbers[i]))
        {
            return 0;
        }
    }
    return 1;
}
int main()
{
    int t;
    unsigned int a,b;
    cin>>t;
    generate();
    while(t--)
    {
        cin>>a>>b;
        if(a==1)
        {
            a++;
        }
        while(a<=b)
        {
            if(prime(a))
            {
                cout<<a;
       
            a++;
        }
        cout<<endl;
    }
}

CAN SOME ONE EXPLAIN ME THE WORKING OF THIS CODE

  • created

    Oct '15
  • last reply

    May '20
  • 6

    replies

  • 3.9k

    views

  • 4

    users

  • 1

    link

Please update your post by using preformatted text tags around your code.

What is your problem?

though i am using preformatted text i am getting the same, any how can u explain me working of my above code everything is fine only print is missing in my code there

What don't you understand about it? Just giving you the answer won't help you learn.

In fact, I should chastise you for not attempting to write your own solution to the problem instead of just googling someone else's answer. smile

2 years later
  #include <iostream>
#include <cmath>

using namespace std;

//function to check if the number is prime or not
int ifprime(int value){
  //if value is 1 or 0 simply return 0
  if(value == 1|| value == 0){
      return 0;
  }else if(value == 2){
    //if value is 2 then return 1
    return 1;
  }else{
    //use cmath for sqrt function
    for(int i=2; i<=sqrt(value); i++){
  		if((value%i) == 0){
        //if value is divisible by any other number return 0
  			return 0;
  		}
  	}
    //if value is not divisible by any other number return 1
    return 1;
  }

}

int main() {
	// your code here
  //we know that at a max there will be 10 test cases, so there will be 10 lowerlimits and upperlimits.
  //so I decided to use an array for lowerlimits and upperlimits
	int testcases,ll,ul,lowerlimit[11],upperlimit[11];

  //read the number of testcases
	cin>>testcases;
  cout<<endl;

  //for each test case, read the lower and upper limit and then assign them to the arrays
	for(int i=0;i<testcases;i++){
		cin>>ll>>ul;

    //store values in an array
    lowerlimit[i] = ll;
    upperlimit[i] = ul;

	}

  //for each test case
  for(int j = 0 ; j<testcases; j++){
    //read the lower limit and upper limit of that test case from the array
    int min = lowerlimit[j];
    int max = upperlimit[j];

    cout<<endl;

    //then for each value within the limit check if it is prime or not
    for(int k=min;k<=max;k++){
      //the if prime function is created above
       if(ifprime(k)){
         cout<<k<<endl;
       }
     }
  }

	return 0;
}
2 years later

its true …