fork(1) download
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <math.h>
  4.  
  5. int main(void) {
  6. int count;
  7. int i;
  8. int j;
  9. int k;
  10. int c;
  11. int prime;
  12. clock_t start;
  13. clock_t end;
  14. double elapsed;
  15. int primes[100];
  16.  
  17. /* init primes */
  18. primes[0] = 2;
  19. c = 1;
  20.  
  21. /* input */
  22. scanf("%d", &count);
  23.  
  24. /* measure time */
  25. start = clock();
  26. for (i = 3; c < count; i += 2) {
  27. /* isPrime */
  28. k = sqrt(i);
  29. prime = 1;
  30. for (j = 0; primes[j] <= k; j++) {
  31. if (i % primes[j] == 0) {
  32. prime = 0;
  33. break;
  34. }
  35. }
  36. if (prime) {
  37. primes[c] = i;
  38. c = c + 1;
  39. }
  40. }
  41. end = clock();
  42. elapsed = end - start;
  43.  
  44. /* output */
  45. for (i = 0; i < count; i++) {
  46. printf("%d ", primes[i]);
  47. }
  48. printf("\n");
  49. printf("elapsed time:%f\n", elapsed);
  50.  
  51. return 0;
  52. }
Success #stdin #stdout 0.02s 1724KB
stdin
10
stdout
2 3 5 7 11 13 17 19 23 29 
elapsed time:0.000000