Advertisement
Guest User

Untitled

a guest
Jul 19th, 2010
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <ctime>
  4.  
  5. void get_sum_div(int min, int max, std::vector<std::pair<int,int>>& v)
  6. {
  7.     const int size = max - min;
  8.     v.resize(size);
  9.     int count = 0;
  10.     for(int index = 0; index < size; index++)
  11.     {
  12.         int sum = 1;
  13.         int n = min + index;
  14.         int divmax = n / 2;
  15.         for(int i = 2; i <= divmax; i++)
  16.         {
  17.             if(!(n % i))
  18.             {
  19.                 sum += i;
  20.             }
  21.         }
  22.         if(sum >= min)
  23.         {
  24.             v[count] = std::pair<int,int>(n, sum);
  25.             count++;
  26.         }
  27.     }
  28.     v.resize(count + 1);
  29. }
  30.  
  31. int main()
  32. {
  33.     time_t begin = time(0);
  34.     const int min = 1000001;
  35.     const int max = 1500000;
  36.  
  37.     std::vector<std::pair<int,int>> v;
  38.     get_sum_div(min, max, v);
  39.     time_t done = time(0);
  40.     std::cout << "get_sum_div() done in " << done - begin << " s." << std::endl;  
  41.  
  42.     std::vector<std::pair<int,int>>::const_iterator end = v.end();
  43.     std::vector<std::pair<int,int>>::iterator i1;
  44.     std::vector<std::pair<int,int>>::iterator i2;
  45.    
  46.     for(i1 = v.begin(); i1 != end; ++i1)
  47.     {
  48.         for(i2 = i1 + 1; i2 != end; ++i2)
  49.         {
  50.             if((i1->first == i2->second) && (i1->second == i2->first))
  51.             {
  52.                 time_t found = time(0);
  53.                 std::cout << i1->first << " " << i2->first
  54.                     << " found in " << found - begin << " s." << std::endl;
  55.             }
  56.         }
  57.     }
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement