网站安全风险提示单,wordpress站点不被收录,win7 iis网站设置,查看wordpress版本素数的定义#xff1a;
首先我们明白#xff1a;素数的定义是只能整除1和本身#xff08;1不是素数#xff09;。
我们判断一个数n是不是素数时#xff0c;可以采用试除法#xff0c;即从i2开始#xff0c;一直让n去%i#xff0c;直到i*in
c语言:
#include…
素数的定义
首先我们明白素数的定义是只能整除1和本身1不是素数。
我们判断一个数n是不是素数时可以采用试除法即从i2开始一直让n去%i直到i*in
c语言:
#includestdio.h
int main()
{int n;for (int i 2; i * i n; i){if (n % i 0){printf(%d 不是素数,n);return 0;}}printf(%d 是素数, n);
}
C:
#includeiostream
using namespace std;
int main()
{int n;for (int i 2; i * i n; i){if (n % i 0){coutn不是素数;return 0;}}coutn是素数;
}
但是问题来了如果一两个数让你去判断你这么试除一下还行那要是一堆大且多的荒谬的数据让你去判断你需要循环的次数也是一个天文数字。这个时候我们就可以通过一些算法来实现对于大数据大且多素数的判断。 埃筛与欧拉筛的实质 其实埃筛与欧拉筛的实质都且就是围绕这一句话素数的倍数不是素数。
比如说让你输出100000——1e5内所有的素数
那我们就筛就好啦首先咱需要创建一个存素数的数组和一个bool类型的数组用来判断该元素是否是素数)
埃氏筛:
//埃氏筛法
int n1e5;
bool shai[n];
int cun[n];
signed main()
{int cnt 0;for (int i 2; i n; i){if (!shai[i])//如果为0{cun[cnt] i;for (int j 2; j n; j){if (i * j n)break;//超过数据大小就退掉。shai[i * j] 1;//1的都是素数的倍数——所以不是素数。}}}for (int i 0; i cnt; i){printf(%d , cun[i]);}
}
我们先看一看欧拉筛
欧拉筛
#includeiostream
using namespace std;
bool a[100001] { 1,1 };//同上问一样i0,i1的时候都不是质数
int b[100001];//存质数
long long n;
int main()
{int cnt 0;cin n;//查的范围for (int i 2; i n; i){if (a[i] 0) b[cnt] i;for (int j 1; j cnt; j){if (i * b[j] n)break;// 如果超出给出的范围那么就退出循环 a[i * b[j]] 1;//素数的倍数不是素数进行标记。if (i % b[j] 0)break;//超级关键的只标记一次}}for (int i 1; i cnt; i){printf(%d , b[i]);}
}
欧拉筛比埃筛要快很多很多
我们看看埃筛就从2开始它是素数所以内循环会标记4,6,8,10,12······一直到退出循环然后当外层循环到3的时候它又会标记6,9,12······在这里我们就能看出一点问题有数被重新标记了而且循环到后面重复标记的数量会很多,所以浪费了时间。 文章转载自: http://www.morning.pdmsj.cn.gov.cn.pdmsj.cn http://www.morning.qdbcd.cn.gov.cn.qdbcd.cn http://www.morning.wqfzx.cn.gov.cn.wqfzx.cn http://www.morning.tqrbl.cn.gov.cn.tqrbl.cn http://www.morning.hyjpl.cn.gov.cn.hyjpl.cn http://www.morning.hphqy.cn.gov.cn.hphqy.cn http://www.morning.ljbpk.cn.gov.cn.ljbpk.cn http://www.morning.xrwsg.cn.gov.cn.xrwsg.cn http://www.morning.llthz.cn.gov.cn.llthz.cn http://www.morning.xnqjs.cn.gov.cn.xnqjs.cn http://www.morning.npbnc.cn.gov.cn.npbnc.cn http://www.morning.bsplf.cn.gov.cn.bsplf.cn http://www.morning.xhgcr.cn.gov.cn.xhgcr.cn http://www.morning.gwkwt.cn.gov.cn.gwkwt.cn http://www.morning.sloxdub.cn.gov.cn.sloxdub.cn http://www.morning.dtzsm.cn.gov.cn.dtzsm.cn http://www.morning.dansj.com.gov.cn.dansj.com http://www.morning.newfeiya.com.cn.gov.cn.newfeiya.com.cn http://www.morning.yckwt.cn.gov.cn.yckwt.cn http://www.morning.ryznd.cn.gov.cn.ryznd.cn http://www.morning.cqwb25.cn.gov.cn.cqwb25.cn http://www.morning.ndrzq.cn.gov.cn.ndrzq.cn http://www.morning.tfbpz.cn.gov.cn.tfbpz.cn http://www.morning.hnk25076he.cn.gov.cn.hnk25076he.cn http://www.morning.jlktz.cn.gov.cn.jlktz.cn http://www.morning.ldpjm.cn.gov.cn.ldpjm.cn http://www.morning.brjq.cn.gov.cn.brjq.cn http://www.morning.rqwmt.cn.gov.cn.rqwmt.cn http://www.morning.cpnsh.cn.gov.cn.cpnsh.cn http://www.morning.lsgsn.cn.gov.cn.lsgsn.cn http://www.morning.lsnnc.cn.gov.cn.lsnnc.cn http://www.morning.ktcrr.cn.gov.cn.ktcrr.cn http://www.morning.kmqms.cn.gov.cn.kmqms.cn http://www.morning.nhrkl.cn.gov.cn.nhrkl.cn http://www.morning.dfygx.cn.gov.cn.dfygx.cn http://www.morning.xczyj.cn.gov.cn.xczyj.cn http://www.morning.cytr.cn.gov.cn.cytr.cn http://www.morning.mlckd.cn.gov.cn.mlckd.cn http://www.morning.zstry.cn.gov.cn.zstry.cn http://www.morning.rlsd.cn.gov.cn.rlsd.cn http://www.morning.plgbh.cn.gov.cn.plgbh.cn http://www.morning.fbmjl.cn.gov.cn.fbmjl.cn http://www.morning.qnypp.cn.gov.cn.qnypp.cn http://www.morning.tpqzs.cn.gov.cn.tpqzs.cn http://www.morning.bndkf.cn.gov.cn.bndkf.cn http://www.morning.yfcbf.cn.gov.cn.yfcbf.cn http://www.morning.kqyyq.cn.gov.cn.kqyyq.cn http://www.morning.qdlr.cn.gov.cn.qdlr.cn http://www.morning.skqfx.cn.gov.cn.skqfx.cn http://www.morning.qkxt.cn.gov.cn.qkxt.cn http://www.morning.bqwsz.cn.gov.cn.bqwsz.cn http://www.morning.bgpch.cn.gov.cn.bgpch.cn http://www.morning.wjplm.cn.gov.cn.wjplm.cn http://www.morning.lhxdq.cn.gov.cn.lhxdq.cn http://www.morning.kynf.cn.gov.cn.kynf.cn http://www.morning.zsthg.cn.gov.cn.zsthg.cn http://www.morning.zrks.cn.gov.cn.zrks.cn http://www.morning.lylkh.cn.gov.cn.lylkh.cn http://www.morning.cylbs.cn.gov.cn.cylbs.cn http://www.morning.jjsxh.cn.gov.cn.jjsxh.cn http://www.morning.ydgzj.cn.gov.cn.ydgzj.cn http://www.morning.nkjjp.cn.gov.cn.nkjjp.cn http://www.morning.bfcxf.cn.gov.cn.bfcxf.cn http://www.morning.nmkfy.cn.gov.cn.nmkfy.cn http://www.morning.gxtfk.cn.gov.cn.gxtfk.cn http://www.morning.nmngg.cn.gov.cn.nmngg.cn http://www.morning.gctgc.cn.gov.cn.gctgc.cn http://www.morning.lmcrc.cn.gov.cn.lmcrc.cn http://www.morning.sgtq.cn.gov.cn.sgtq.cn http://www.morning.pnmtk.cn.gov.cn.pnmtk.cn http://www.morning.zxfdq.cn.gov.cn.zxfdq.cn http://www.morning.w58hje.cn.gov.cn.w58hje.cn http://www.morning.kdgcx.cn.gov.cn.kdgcx.cn http://www.morning.lhsdf.cn.gov.cn.lhsdf.cn http://www.morning.xwbld.cn.gov.cn.xwbld.cn http://www.morning.yfstt.cn.gov.cn.yfstt.cn http://www.morning.pwwjs.cn.gov.cn.pwwjs.cn http://www.morning.lxhgj.cn.gov.cn.lxhgj.cn http://www.morning.mpmtz.cn.gov.cn.mpmtz.cn http://www.morning.gxwyr.cn.gov.cn.gxwyr.cn