回复 32楼 beyondyf
各有千秋 表示学习 !
梅尚程荀
马谭杨奚
/* Program 7.11 A dynamic prime example */ #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <time.h> int main(void) { unsigned long *primes = NULL; unsigned long trial = 0; size_t i = 0; int start = 0; bool found = false; size_t total = 0; size_t count = 0; start = clock(); printf("How many 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 = 0 ; i < count ; i++) if(!(found = (trial % *(primes+i)))) break; if(found) *(primes+count++) = trial; } printf("\ntime: %lf\n",((double)clock()-start)/CLOCKS_PER_SEC); return 0; }