对这个问题有点不解,程序是做出来了,运行也对,但是为什么输出5位和5位以上的数字就运行不了了呢?
#include <stdio.h>#include <stdbool.h>
#include <stdlib.h>
int main(void)
{
unsigned long *p=NULL;
unsigned long trial=0;
bool found=false;
size_t total=0;
size_t count=0;
size_t i=0;
printf(" How many primes would you like - you'll get at least 4? ");
scanf("%u",&total);
total=total<4U ? 4U:total;
p=(unsigned long *)malloc(total*sizeof(unsigned long));
if(p==NULL)
{
printf("\n This is a error.\n");
return 1;
}
*p=2UL;
*(p+1)=3UL;
*(p+2)=5UL;
count=3U;
trial=5U;
while(count<total)
{
trial+=2UL;
for(i=0;i<count;i++)
if(!(found=(trial%*(p+i))))
break;
if(found)
*(p+count++)=trial;
}
for(i=0;i<count;i++)
{
if(!(i%5U))
printf("\n");
printf("%12lu",*(p+i));
}
printf("\n");
return 0;
}
顺便说一下,这是一个显示质数的程序。