fork download
  1. /* clock example: frequency of primes */
  2. #include <stdio.h> /* printf */
  3. #include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */
  4. #include <math.h> /* sqrt */
  5.  
  6. int frequency_of_primes (int n) {
  7. int i,j;
  8. int freq=n-1;
  9. for (i=2; i<=n; ++i) for (j=sqrt(i);j>1;--j) if (i%j==0) {--freq; break;}
  10. return freq;
  11. }
  12.  
  13. int main ()
  14. {
  15. clock_t t;
  16. int f;
  17. t = clock();
  18. printf ("Calculating...\n");
  19. f = frequency_of_primes (99999);
  20. printf ("The number of primes lower than 100,000 is: %d\n",f);
  21. t = clock() - t;
  22. float diff = ((float)t)/CLOCKS_PER_SEC;
  23. printf ("It took me %d clicks (%f milliseconds).\n",t,diff*1000);
  24. return 0;
  25. }
  26.  
Success #stdin #stdout 0.26s 2248KB
stdin
Standard input is empty
stdout
Calculating...
The number of primes lower than 100,000 is: 9592
It took me 260000 clicks (260.000000 milliseconds).