连续质数计算C语言(c语言求质数的循环算法)
C语言题(连续质数和)
简单写了一下 供参考
#include?stdio.h
static?char?prime[10001];
static?int?sum[10001];
static?int?cnt[10001];
void?calc_prime(void)
{
int?i,?j;
for(i?=?2;?i??10001;?i?++)
{
if(prime[i])continue;
for(j?=?i*2;?j??10001;?j?+=?i)
prime[j]?=?1;
}
}
int?calc_sum(void)
{
int?i,?j;
for(i?=?2,?j?=?1;?i??10001;?i?++)
{
if(!prime[i])
{
sum[j]?=?sum[j-1]?+?i;
j?++;
}
}
return?j;
}
void?calc_cnt(int?total)
{
int?i,?j,?k;
for(i?=?total?-?1;?i??0;?i?--)
{
for(j?=?i?-?1;?j?=?0;?j?--)
{
k?=?sum[i]?-?sum[j]?;
if(k??10000)?break;
cnt[k]?++;
}
}
}
int?main()
{
int?t,?i;
calc_prime();
calc_cnt(calc_sum());
scanf("%d",?t);
while(t?--)
{
scanf("%d",?i);
printf("%d\n",?cnt[i]);
}
return?0;
}
C语言求1到n所有质数
#includestdio.h
int main()
{ int i,j,n,m;
scanf("%d",n);
for(m=0;n;n/=10)m=m*10+n%10;
if(m1)printf("2 ");
for(i=3; i=m; i+=2)
? { for(j=3;j*j=i;j+=2)
? ? ? if(i%j==0)j=i;
? ? if(j*ji)printf("%d ",i);
? }
return 0;
}
C语言计算质数
#include?stdio.h
static?char?m[8000000];
static?int?prime[500000];
#define?MAX_N?7368787
void?set(void)
{
????int?i,j;
????for(i?=?2;?i=MAX_N;?i++)
????{
????????if(m[i]?==?1)?continue;
????????for(j=i+i;?j=MAX_N;?j+=i)
????????????m[j]=1;
????}
????for(i?=?j=0;?i=MAX_N;i++)
????????if(m[i]==0)?prime[j++]=i;
}
int?main()
{
????int?m,n,;
????while(~scanf("%d%d",m,n))
????{
????????
????????for(i=m;i=n;i++)
????????{
????????????printf("%d",prime[i-1]);
????????????if(i==n?||?(i-m)%10==9)?putchar('\n');
????????????else?putchar('?');
????????}
????}
????return?0;
}