1. 程式人生 > >B1013 數素數

B1013 數素數

+= class bsp find urn each include break cout

#include<iostream>

using namespace std;

const int MAXN = 1000000;
int prime[MAXN];
int num = 0;
bool p[MAXN] = { 0 };

void find_prime(int n)
{
	for (int i = 2; i<MAXN; i++)
	{
		if (p[i] == false)
		{
			prime[num++] = i;
			if (num >= n)
				break;
			for (int j = i + i; j<MAXN; j +=i)//j<maxn,not j<=n
			{
				p[j] = true;
			}
		}
	}
}


int main()
{
	int count = 0;
	int m, n;
	cin >> m >> n;
	find_prime(n);
	for (int i = m; i<n + 1; i++)
	{
		cout << prime[i - 1];//coz array index begin with 0,so the Mth number,which index is m-1
		count++;
		if (count % 10 != 0 && i<n)//10 numbers of each line, good idea!
			cout << " ";
		else
			cout << endl;
	}

	return 0;
}

B1013 數素數