素数筛选法的进一步升级

来源:互联网 发布:数据调研公司排名 编辑:程序博客网 时间:2024/05/24 00:24

今天晚上,正在翻书的时候,想学习一下数论,结果看到了素数筛的一部分,浴室我就温习了一下素数筛选法的代码,

我突然发现在筛素数的时候可以把外层循环缩小到他的根号2倍,嘿嘿,有点高兴啊。。。

上代码:(其实跟以前的差不多就是一样的)

/**2015 - 09 - 25Author: ITAKMotto:今日的我要超越昨日的我,明日的我要胜过今日的我,以创作出更好的代码为目标,不断地超越自己。**/#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <vector>#include <queue>#include <algorithm>#include <set>using namespace std;typedef long long LL;typedef unsigned long long ULL;const int maxn = 1e5+5;const int mod = 1e9+7;const double eps = 1e-7;struct node{    int good, money;} arr[maxn];bool cmp(node a, node b){    return a.money < b.money;}bool vis[maxn];int prime[maxn];void is_prime(){    memset(vis, 1, sizeof(vis));    int m = (int)sqrt(0.5+maxn);    for(int i=2; i<=m; i++)    {        if(vis[i])            for(int j=i*i; j<=maxn; j+=i)                vis[j] = 0;    }    ///执行程序(也就是实验)    for(int i=2; i<maxn; i++)        if(vis[i])            cout<<i<<" ";}int main(){    is_prime();    return 0;}


0 0
原创粉丝点击