Advertisement
Delfigamer

Untitled

Oct 17th, 2015
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. #include <random>
  2. #include <vector>
  3. #include <ctime>
  4. #include <cmath>
  5. #include <cstdio>
  6.  
  7. std::vector< int > primes;
  8. int primes_end = 2;
  9.  
  10. void fillprimes( int newend )
  11. {
  12.     for( int val = primes_end; val < newend; ++val )
  13.     {
  14.         bool isprime = true;
  15.         for( auto it: primes )
  16.         {
  17.             if( val % it == 0 )
  18.             {
  19.                 isprime = false;
  20.                 break;
  21.             }
  22.         }
  23.         if( isprime )
  24.         {
  25.             primes.push_back( val );
  26.         }
  27.         if( val % 10000 == 0 )
  28.         {
  29.             printf( "\t\t%i\n", val );
  30.         }
  31.     }
  32.     primes_end = newend;
  33. }
  34.  
  35. int divprod( int target )
  36. {
  37.     int result = 1;
  38.     int tend = ceil( sqrt( target ) );
  39.     // fillprimes( tend + 1 );
  40.     // for( auto it : primes )
  41.     // {
  42.         // if( it > tend )
  43.         // {
  44.             // break;
  45.         // }
  46.         // else if( target % it == 0 )
  47.         // {
  48.             // result *= it;
  49.         // }
  50.     // }
  51.     for( int i = 2; i <= tend; ++i )
  52.     {
  53.         if( target % i != 0 )
  54.         {
  55.             continue;
  56.         }
  57.         bool isprime = true;
  58.         for( int j = 2; j < i; ++j )
  59.         {
  60.             if( i % j == 0 )
  61.             {
  62.                 isprime = false;
  63.                 break;
  64.             }
  65.         }
  66.         if( isprime )
  67.         {
  68.             result *= i;
  69.         }
  70.     }
  71.     if( result == 1 )
  72.     {
  73.         result = target;
  74.     }
  75.     return result;
  76. }
  77.  
  78. int main( int argc, char** argv )
  79. {
  80.     static int const test[] = {
  81.         1,
  82.         2*2*2*2*5*5*5*7*7*11,
  83.         127*127,
  84.         1039,
  85.     };
  86.     for( int i = 0; i < int( sizeof( test ) / sizeof( test[ 0 ] ) ); ++i )
  87.     {
  88.         printf( "%i\t%i\n", test[ i ], divprod( test[ i ] ) );
  89.     }
  90.     int volatile* res = new int[ 100000 ];
  91.     std::mt19937 rand( 123 );
  92.     clock_t t = clock();
  93.     for( int i = 0; i < 100000; ++i )
  94.     {
  95.         int val = rand() & 0xffffff;
  96.         int dp = divprod( val );
  97.         res[ i ] = dp;
  98.     }
  99.     printf( "%f", float( clock() - t ) / CLOCKS_PER_SEC );
  100.     delete[] res;
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement