Prime number

Topic description

(Again: all the questions on the computer are automatically judged right or wrong by the program, so please do not appear if there is no required output, please refer to the sample input and output for details)

Input two positive integers a and b, find the prime numbers between [a, b], and output from small to large. (2 <= a <= b <=1000000)

enter description

Enter two positive integers per line, with a space between the two numbers, there may be multiple lines of input!

Tips: Use while(cin>>a>>b) statement to realize loop input variable

output description

Output the prime numbers between [a,b], the prime numbers are separated by a space, each output occupies a line

sample input

 

2 5
3 11

Sample output

 

2 3 5
3 5 7 11
#include <iostream>
using namespace std;

intmain ()
{
    bool q[1000001];
    for (int i = 0; i < 1000001;i++)
        q[i] = true;
    for (int d = 2;d * d <= 1000000; d++)
    {
        if(q[d])
        {
            for(int s = d * d;s <= 1000000; s+=d)
                q[s] = false;
        }
    }
    int a, b;
    while (cin >> a >> b)
    {
        int g = 0;
        for (int i = a; i <= b; i++)
        {
            if (q[i])
            {
            if (!g)
            {
                cout << i;
                g = 1;
            }
            else 
                cout << ' ' << i;
            }
       }
       cout << endl;
    }
    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325075269&siteId=291194637