Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include "bits stdc++.h"

using namespace std;
const int maxn=50000;

int fa[maxn],p[maxn];
int tst,ct,m,n;
int vi[101000];

void prep()
{
    ct=0;
    for( int i=2; i < maxn; i++ )
        if( ! fa[i] )
        {
            p[++ct]=i;
            for( int j=2; j <= (maxn-1)/i; j++ )
                fa[i*j]=i;
        }
}

void work()
{
    scanf( "%d%d", &m, &n );
    if( m == 1 )
        m++;
    int lim= (int)sqrt( n ) + 1;
    for( int i = 1; i <=ct; i++ )
    {
        if( p[i] > lim )
            break;
        int l = ( m - 1 ) / p[i] + 1;
        if( l == 1 )
            l++;
        int r = n / p[i];
        for( int j = l; j <= r; j++ )
            vi[ j * p[i] - m ] = tst;
    }
    for( int i = 0; i <= n - m; i++ )
    if( vi[i] != tst )
        printf( "%d\n", i + m );
}

int main()
{
    scanf( "%d", &tst );
    prep();
    while( tst )
    {
        work();
        tst--;
        if( tst )
            puts("");
    }
    return 0;
}


What I have tried:

I am trying to learn about sieve of eratothenes and came across this code, is this an example of sieve of eratothenes?
Posted
Updated 5-Apr-20 7:44am
v3
Comments
Patrice T 4-Apr-20 11:33am    
The code you pasted is corrupted.
Rick York 5-Apr-20 13:45pm    
It is an example of rather bad code because it has global variables that are one and two characters long. That is very, VERY bad practice. I see no need for any of those variables to be at global scope in the first place.

Try it and see what it does.
Or ... go back to where you got it and ask there.

Finding random pieces of software on the internet and asking "does it do this?" on a totally different site is not a good way to learn: instead, got to Wiki, read the article on the Sieve of Eratosthenes[^] and try writing your own. You will learn a whole lot more that way, as people learn by doing, not by looking at an example. A random chunk of code tells you nothing about why it is the way it is, what alternatives were considered, why they were discarded.

And if this is a homework assignment - and this is never implemented in the real world so I'm pretty sure it is - then learning how to design and code to a specification is why it was set ...
 
Share this answer
 
Your code is badly copied from somewhere and so full of bugs. Search in the internet for some explanation and some tutorial like Learn C++ to learn coding in C++.

Learn coding - that is your homework.
 
Share this answer
 
Comments
Patrice T 4-Apr-20 13:47pm    
"Your code is badly copied from somewhere and so full of bugs."
as I have seen this problem many times, I guess that there is a combination of
particular editor/Copy > particular browser/paste
that corrupt the code pasted.
KarstenK 6-Apr-20 3:12am    
Looks like he fixed the "bad copy" bug :-O

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900