top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to find prime numbers between given intervals?

+2 votes
541 views

How to find prime numbers between given intervals? Sample code along with the complexity would be helpful?

posted Feb 22, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Prime Numbers between given intervals:

 #include<bits/stdc++.h>
  using namespace std;
  int main()
{
  int Counter,NumOne,NumTwo,count,i;
  scanf("%d %d",&NumOne,&NumTwo);
     for (Counter = NumOne;Counter <= NumTwo;Counter++)
      {
            count = 0;
            for ( i = 2; i <=Counter/2; i++)
          {
                if(Counter%i==0)
              {
                    count++;
                    break;
                }
            }
            if(count==0 && Counter!=1){
              printf("%d",Counter);
            }
        }
return 0;
}

Complexity = O(n^2)
We can reduce this to nlog(n) comment if you need that code.

answer Feb 26, 2016 by Shivam Kumar Pandey
Shivam if you can help me with the code of nlog(n) complexity it would be helpful please share
okay shashi asap.
Similar Questions
0 votes

Given an unordered array A of size n and integer x. What is the best complexity to find two elements in A whose sum is x?
Share the algo, its complexity and if possible C code.

+5 votes

Example :
Let the list be {2,3,5} and Assume always 1 be included then

2th number is 2
3th number is 3
4th number is 4
5th number is 5
6th number is 6
7th number is 8
8th number is 9
9th number is 10
10th number is 12
11th number is 15 and so on...

...