筛素数初学

 

QAQ 温习 


#include<iostream>
using namespace std;

//首先筛掉里面每个数的倍数
//2- p-1 没有被筛掉 就是他前面没有它的因子

const int N = 100000;
int primes[N];
int cnt;
bool st[N];//是否被筛掉
void get_prime(int n)
{
	//从2到n枚举
	for (int i = 2; i <= n; i++)
	{
		//如果当前的数没有被筛过的话,说明它是一个质数
		if (!st[i])
			primes[cnt++] = n;//存储质数
		//然后再删掉每个数的倍数
		for (int j = i + i; j <= n; j += i)
			st[j] = true;

	}
}

猜你喜欢

转载自blog.csdn.net/m0_62894677/article/details/130258106