一个求质数程序,运行结果不正确,编译没错误
代码如下#include<stdio.h>
#include<stdlib.h>
int main(void)
{
unsigned long *primes=NULL;
unsigned long trial=0;
size_t total=0;
size_t count=0;
size_t i=0;
int found=0;
printf("Howmany primes would you like -you'll get at least 4?");
scanf("%u",&total);
total=total<4U?4U:total;
primes=(unsigned long *)malloc(total*sizeof(unsigned long));
if(primes==NULL)
{
printf("\nNot enough memory.Hasta la Vista,baby.\n");
return 1;
}
*primes=2UL;
*(primes+1)=3UL;
*(primes+2)=5UL;
count=3U;
trial=5U;
while(count<total)
{
trial+=2UL;
for(;i<count;i++)
if(!(found=(trial%*(primes+i))))
break;
if(found)
*(primes+count++)=trial;
}
for(;i<total;i++)
{
if(!(i%5U))
printf("\n");
printf("%12lu",*(primes+i));
}
printf("\n");
getch();
}