
#include <stdioh>
#inlcude <mathh>
void main(){
int m, k;
puts("please input the number:");
scanf_s("%d", &m);
printf("the prime numbers include as following: ");
for (int j = 1; j <= m; j++) {
k = sqrt(j);
for (int i = 2; i <= k+1; i++) {
if (j%i == 0) {
break;
}
if (i == k + 1) {
printf("%d ", j);
}
}
}
}
输入值是100就可以求100以下的质数,为了你方便我没有把数字写死。
#include <stdioh>
#include <mathh>
#include <timeh>
#include <memoryh>
#define BLOCKSIZE 150150
#define ST 6
//23571113 5 = 150150
//#define BLOCKSIZE 510510 // 2357111317=510510
clock_t tstart;
void Send2Log(__int64 nMax, __int64 PrimeCnt){
double duration = (double)(clock() - tstart) / CLOCKS_PER_SEC;
printf(">%73f秒\t计算到%11I64u\t查出素数:%10I64u个\n", duration, nMax, PrimeCnt);
}
int main()
{
int i, j, p;
// unsigned int QRT; //the square root of the end of current block
// unsigned int Start; //the first multiple of a prime in current block
unsigned int BasePrime[BLOCKSIZE] = { 2 }; //store the first prime block
unsigned __int64 PrimeCnt = 1; //prime counter
unsigned __int64 KPoint = 0; //the start of current block
bool BaseTpl[BLOCKSIZE]; //the block template
bool CurBuf[BLOCKSIZE]; //current block
tstart = clock();
memset(BaseTpl, true, BLOCKSIZE);
// int bqrt = (int)sqrt(BLOCKSIZE) + 1;
BaseTpl[1] = false;
for (i = 1; i i < BLOCKSIZE; i += 2){
if (BaseTpl[i] == true){
BasePrime[PrimeCnt++] = i;
for (j = i i; j < BLOCKSIZE; j += i 2)
BaseTpl[j] = false;
}
}
while (i < BLOCKSIZE){
if (BaseTpl[i] == true)
BasePrime[PrimeCnt++] = i;
i += 2;
}
//printf("prime number = %I64d, max primer = %d \n",PrimeCnt,BasePrime[PrimeCnt-1] );
memset(BaseTpl, true, BLOCKSIZE);
for (i = 1; i < ST; i++){
for (p = BasePrime[i]; p < BLOCKSIZE; p += BasePrime[i] 2)
BaseTpl[p] = false;
}
for (j = 1; j < BLOCKSIZE; j++){
memcpy(CurBuf, BaseTpl, BLOCKSIZE);
//KPoint = (unsigned __int64)k BLOCKSIZE;
KPoint += BLOCKSIZE;
const unsigned int QRT = (unsigned int)sqrt((long double)(KPoint + BLOCKSIZE)) + 1;
if (j % 1000 == 0)
Send2Log(KPoint, PrimeCnt);
for (i = ST; p = BasePrime[i], (unsigned)p < QRT; p = BasePrime[++i]){ //the 7th prime is 19
unsigned int Start = (unsigned int)(p - (KPoint - 1) % p) - 1;
const unsigned int p2 = p << 1;
if ((Start & 1) == 0)
Start += p;
// if ((unsigned __int64)p p > KPoint)
// Start = (unsigned __int64)p p - KPoint;
while (Start < BLOCKSIZE){
CurBuf[Start] = false;
Start += p2;
}
}
for (i = 1; i < BLOCKSIZE; i += 2){
if (CurBuf[i] == true)
//if(PrimeCnt < BLOCKSIZE) BasePrime[PrimeCnt] = KPoint + i;
PrimeCnt++;
}
}
// Send2Log(KPoint, PrimeCnt);
return 0;
}
运算效果:
> 1250秒 计算到 150150000 查出素数: 8452428个
> 2562秒 计算到 300300000 查出素数: 16267724个
> 3906秒 计算到 450450000 查出素数: 23875495个
> 5281秒 计算到 600600000 查出素数: 31354503个
> 6656秒 计算到 750750000 查出素数: 38739831个
> 8062秒 计算到 900900000 查出素数: 46052992个
> 9500秒 计算到 1051050000 查出素数: 53307936个
1该程序会一直计算下去
2计算300300000 内的素数只需2562秒 ,绝对满足你的要求
3程序我也不怎么懂!!!
筛法求质数:
用筛法求质数的基本思想是:把从1开始的、某一范围内的正整数从小到大顺序排列, 1不是质数,首先把它筛掉。剩下的数中选择最小的数是质数,然后去掉它的倍数。依次类推,直到筛子为空时结束。如有:
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
1不是质数,去掉。剩下的数中2最小,是质数,去掉2的倍数,余下的数是:
3 5 7 9 11 13 15 17 19 21 23 25 27 29
剩下的数中3最小,是质数,去掉3的倍数,如此下去直到所有的数都被筛完,求出的质数为:
2 3 5 7 11 13 17 19 23 29
扩展资料:
质数被利用在密码学上,所谓的公钥就是将想要传递的信息在编码时加入质数,编码之后传送给收信人,任何人收到此信息后,若没有此收信人所拥有的密钥,则解密的过程中,将会因为找质数的过程过久,使即使取得信息也会无意义。
在汽车变速箱齿轮的设计上,相邻的两个大小齿轮齿数设计成质数,以增加两齿轮内两个相同的齿相遇啮合次数的最小公倍数,可增强耐用度减少故障。。
参考资料:
百度百科--筛法求素数
又是我,看到你提了两次问题,刚才应该都弄懂了吧……能否让我再赚点分数?
#include<stdioh>
void main()
{
int i,m;
for(i=3;i<=100;i++)
{
for(m=2;m<i;m++)
{
if(i%m==0) break;/有被整除的数,提前退出,省时间也为后面做准备/
}
if(m==i) printf("%d ",i);/循环正常退出,说明找到了质数/
}
}
不懂的话可以在这里继续问
不知道你是怎么想的; 大脑那么简单,还想同时做多样事情,
1,每一次循环时,a[i+1]根本还没初始化到,只初始化了a[i],你却想在初始化前面一个元素的时候就判断后面一个没初始化的数是不是质数
2,按你的注释想法,检查那个元素是不是质数应该用2到他本身的所有整数进行测试
可是,好像你有想用所有小于它的质数测试(但也不是那样写的)
一句话,要脚踏实地!!!
你可以看看答案,然后关闭答案写一遍,会了在想其他方法
没有什么好的办法,如果用程序,就计算n除以2到根号n最接近的整数,如果都不能整除,n就是质数
比如101,要计算19除以2,3,4,5直到10,如果都不能整除,就是质数。
如果你要手动计算,就挨个写,2,3,5,7,11,13,如果数字足够大,不需要像程序一样挨个除,只需要除以比它小的质数就可以了。
以上就是关于用C语言编写一个程序,输出1到100中的质数全部的内容,包括:用C语言编写一个程序,输出1到100中的质数、求一个求质数的程序C++、求质数方法等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)