betoesquivel

using example millisecond clock

Nov 30th, 2013
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include <chrono>
  2. #include <iostream>
  3.  
  4. int frequency_of_primes (int n) {
  5.     int freq=n-1;
  6.  
  7.     for (int i=2; i<=n; ++i)
  8.         for (int j=sqrt(i);j>1;--j)
  9.             if (i%j==0)
  10.             {
  11.                 --freq;
  12.                 break;
  13.             }
  14.  
  15.     return freq;
  16. }
  17.  
  18.  
  19.  
  20. int main ()
  21. {
  22.     using namespace std::chrono ;
  23.  
  24.     std::cout << "Calculating...\n" ;
  25.    
  26.     auto begin = high_resolution_clock::now() ;
  27.     int f = frequency_of_primes (99999);
  28.     auto end = high_resolution_clock::now() ;
  29.  
  30.     auto ticks = duration_cast<microseconds>(end-begin) ;
  31.  
  32.     std::cout << "The number of primes lower than 100,000 is: " << f << '\n' ;
  33.     std::cout << "It took me " << ticks.count() << " microseconds.\n" ;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment