MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / main

Function main

Spoj_problems/PRIME1/solution.cpp:43–91  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

41}
42
43int main()
44{
45 int T;//the number of test cases
46 cin>>T;
47 int i,p=0,s;
48
49 int N;//larger number
50 int M;//smaller number
51 bool primesNow[DIFF_SIZE];
52
53 while(T--)
54 {
55 cin>>M>>N; //input the range
56
57 for(i=0;i<DIFF_SIZE;i++)
58 primesNow[i]=true; //initialise to all true . we will mark the composite number as false and then
59 //the remaining true numbers will be prime
60
61 populateMyPrimes(N); //this populates the primes in sqrt(N) in an array myPrimes
62
63 for(i=0;i<cnt;i++) //for each prime in sqrt(N) we need to use it in the segmented sieve process
64 {
65 p=myPrimes[i]; //store the prime
66 s=M/p;
67 s=s*p; //the closest number less than M that is a composite number for this prime p
68
69 for(int j=s;j<=N;j=j+p)
70 {
71 if(j<M) continue; //because composite numbers less than M are of no concern to use.
72 primesNow[j-M]=false;//j-M = index in the array primesNow,this is because max index allowed in the array
73 //is not N ,it is DIFF_SIZE so we are storing the numbers offset from M
74 //while printing we will add M and print to get the actual number
75 }
76 }
77
78 for(int i=0;i<cnt;i++) //in the above loop the first prime numbers for example say 2,3 are also set to false
79 { //hence we need to print them in case they are within range.
80 if(myPrimes[i]>=M && myPrimes[i]<=N) //without this loop you will see that for an range(1,30), 2 and 3 does
81 cout<<myPrimes[i]<<endl; //not get printed
82 }
83
84 for(int i=0;i<N-M+1;++i) // primesNow[]=false for all composite numbers,so prime numbers can be found by checking with true
85 {
86 if(primesNow[i] == true && (i+M)!=1) //i+M != 1 to ensure that for i=0 and M=1 , 1 is not considered a prime number
87 cout<<i+M<<endl; //print our prime numbers in the range
88 }
89 }
90 return 0;
91}

Callers

nothing calls this directly

Calls 1

populateMyPrimesFunction · 0.85

Tested by

no test coverage detected