欢迎来到天天文库
浏览记录
ID:38809838
大小:47.00 KB
页数:5页
时间:2019-06-19
《素数中的素数》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、质数中的质数给定整数N(22、循环,从2*10^num+1开始,到10^(num+1)结束。取这个循环中这个数的首位,进行判断,是否为4、6、8,如果是则加上10^num,再进行后面的循环。因为,4、6、8不是素数,所以排除以这些数为首位的数。循环变量每次加2,因为素数除2外都是奇数。4、调用yprime(i,num),判断数i是否为所求的数。判断原数是否为素数,再依次去掉后面的数,看是否为素数。最后如果全部满足,则返回1,否则返回0;5、判断素数,调用函数prime(n)。程序如下:#include#include#includevoidfun(i3、ntnum);intyprime(intn,intnum);intprime(intn);//n是否是素数intmain(){intnum=0;printf("请输入位数:");scanf("%d",&num);fun(num);return0;}//从后面往前面数,每位是否是素数//如7331,733,73,7都是素数//输出位数num以内的所有满足这一条件的数voidfun(intnum){inti,t,n=1;for(i=1;i4、5、t=6、=67、8、t==8)//首位为4、6、8的数不满足条件i+=n;if(yprime(i,num))//输出满足条件的数printf("%dt",i);}}//满足条件,则返回1,不满足条件,返回0intyprime(intn,intnum){inti;for(i=0;i9、0GHz,988MB)上运行Code::Blocks10.05,得运行结果如下:请输入位数:2232931375359717379Processreturned0(0x0)executiontime:0.266sPressanykeytocontinue.请输入位数:3233239293311313317373379593599719733739797Processreturned0(0x0)executiontime:0.219sPressanykeytocontinue.请输入位数:4233323392393239929393119313737333739379337910、759397193733173337393Processreturned0(0x0)executiontime:0.219sPressanykeytocontinue.请输入位数:5233332333923399239932939931193313793733737339373975939359399719337333173939Processreturned0(0x0)executiontime:0.719sPressanykeytocontinue.请输入位数:623399323993329399937337937339359393359399371933373939111、739393739397739399Processreturned0(0x0)executiontime:1.094sPressanykeytocontinue.请输入位数:723399332399333293999937337995939333739391373939317393933Processreturned0(0x0)executiontime:9.875sPressanykeytocontinue.请输入位数:82339933929399999373379995939333973939133Proces
2、循环,从2*10^num+1开始,到10^(num+1)结束。取这个循环中这个数的首位,进行判断,是否为4、6、8,如果是则加上10^num,再进行后面的循环。因为,4、6、8不是素数,所以排除以这些数为首位的数。循环变量每次加2,因为素数除2外都是奇数。4、调用yprime(i,num),判断数i是否为所求的数。判断原数是否为素数,再依次去掉后面的数,看是否为素数。最后如果全部满足,则返回1,否则返回0;5、判断素数,调用函数prime(n)。程序如下:#include#include#includevoidfun(i
3、ntnum);intyprime(intn,intnum);intprime(intn);//n是否是素数intmain(){intnum=0;printf("请输入位数:");scanf("%d",&num);fun(num);return0;}//从后面往前面数,每位是否是素数//如7331,733,73,7都是素数//输出位数num以内的所有满足这一条件的数voidfun(intnum){inti,t,n=1;for(i=1;i4、5、t=6、=67、8、t==8)//首位为4、6、8的数不满足条件i+=n;if(yprime(i,num))//输出满足条件的数printf("%dt",i);}}//满足条件,则返回1,不满足条件,返回0intyprime(intn,intnum){inti;for(i=0;i9、0GHz,988MB)上运行Code::Blocks10.05,得运行结果如下:请输入位数:2232931375359717379Processreturned0(0x0)executiontime:0.266sPressanykeytocontinue.请输入位数:3233239293311313317373379593599719733739797Processreturned0(0x0)executiontime:0.219sPressanykeytocontinue.请输入位数:4233323392393239929393119313737333739379337910、759397193733173337393Processreturned0(0x0)executiontime:0.219sPressanykeytocontinue.请输入位数:5233332333923399239932939931193313793733737339373975939359399719337333173939Processreturned0(0x0)executiontime:0.719sPressanykeytocontinue.请输入位数:623399323993329399937337937339359393359399371933373939111、739393739397739399Processreturned0(0x0)executiontime:1.094sPressanykeytocontinue.请输入位数:723399332399333293999937337995939333739391373939317393933Processreturned0(0x0)executiontime:9.875sPressanykeytocontinue.请输入位数:82339933929399999373379995939333973939133Proces
4、
5、t=
6、=6
7、
8、t==8)//首位为4、6、8的数不满足条件i+=n;if(yprime(i,num))//输出满足条件的数printf("%dt",i);}}//满足条件,则返回1,不满足条件,返回0intyprime(intn,intnum){inti;for(i=0;i9、0GHz,988MB)上运行Code::Blocks10.05,得运行结果如下:请输入位数:2232931375359717379Processreturned0(0x0)executiontime:0.266sPressanykeytocontinue.请输入位数:3233239293311313317373379593599719733739797Processreturned0(0x0)executiontime:0.219sPressanykeytocontinue.请输入位数:4233323392393239929393119313737333739379337910、759397193733173337393Processreturned0(0x0)executiontime:0.219sPressanykeytocontinue.请输入位数:5233332333923399239932939931193313793733737339373975939359399719337333173939Processreturned0(0x0)executiontime:0.719sPressanykeytocontinue.请输入位数:623399323993329399937337937339359393359399371933373939111、739393739397739399Processreturned0(0x0)executiontime:1.094sPressanykeytocontinue.请输入位数:723399332399333293999937337995939333739391373939317393933Processreturned0(0x0)executiontime:9.875sPressanykeytocontinue.请输入位数:82339933929399999373379995939333973939133Proces
9、0GHz,988MB)上运行Code::Blocks10.05,得运行结果如下:请输入位数:2232931375359717379Processreturned0(0x0)executiontime:0.266sPressanykeytocontinue.请输入位数:3233239293311313317373379593599719733739797Processreturned0(0x0)executiontime:0.219sPressanykeytocontinue.请输入位数:42333233923932399293931193137373337393793379
10、759397193733173337393Processreturned0(0x0)executiontime:0.219sPressanykeytocontinue.请输入位数:5233332333923399239932939931193313793733737339373975939359399719337333173939Processreturned0(0x0)executiontime:0.719sPressanykeytocontinue.请输入位数:6233993239933293999373379373393593933593993719333739391
11、739393739397739399Processreturned0(0x0)executiontime:1.094sPressanykeytocontinue.请输入位数:723399332399333293999937337995939333739391373939317393933Processreturned0(0x0)executiontime:9.875sPressanykeytocontinue.请输入位数:82339933929399999373379995939333973939133Proces
此文档下载收益归作者所有