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