1 / 3
Aug 2021

I have been working on this code for about two days and this is the best i could come up with.
Can someone please tell me what is wrong with my code.
#include
#include
#include
using namespace std;

int main()
{
// your code goes here
ios_base::sync_with_stdio(false);
int t;
long int a, b,n, j = 0;
cin >> t;
for (int k = 0; k < t; k++)
{
cin >> a >> b;
n = sqrt(b);

    bool check_primes[n + 1];
    memset(check_primes, true, n+1);
    bool print_primes[b - a + 1];
    memset(print_primes, true, b - a + 1);

    if (a == 1)
        print_primes[0] = false;

    for (long int i = 2; i <= n; i++)
    {
        if (check_primes[i] == true)
        {
            for (long int j = i * i; j <= b; j += i)
            {
                check_primes[j] = false;
            }
            if (a % i == 0 and i!=a)
                j = a;
            else 
                j = a + i - (a % i);
            for (; j <= b; j += i)
            {
                print_primes[j-a] = false;
            }
        }
    }
    for (int l = 0; l <= b - a; l++)
    {
        if (print_primes[l] == true)
            cout << l + a << "\n";
    }

    cout << "\n";
}
return 0;

}

  • created

    Aug '21
  • last reply

    Aug '21
  • 2

    replies

  • 573

    views

  • 2

    users

What happens when j is greater that n?

It seems like I have a talent for over-complicating things.
Thank You so much, for your help.