fork download
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. int divisorsSum( int x )
  5. {
  6. int result = 1;
  7. int sq= (int) sqrt(x)+1;
  8. for ( int d = 2; d <= sq; ++d )
  9. {
  10. if ( x % d == 0 )
  11. {
  12. result += d + x/d;
  13. }
  14. }
  15.  
  16. return result;
  17. }
  18.  
  19. void findNFriends( int n )
  20. {
  21. int findedCount = 0;
  22. for ( int f = 0; ; ++f )
  23. {
  24. int s = divisorsSum( f );
  25. if ( (f < s) && (f == divisorsSum( s )))
  26. {
  27. ++findedCount;
  28. std::cout << findedCount << ": (" << f << ", " << s << ")" << std::endl;
  29.  
  30. if ( findedCount >= n ) break;
  31. }
  32. }
  33. }
  34.  
  35. int main() {
  36. findNFriends(14);
  37. return 0;
  38. }
Success #stdin #stdout 0.53s 2724KB
stdin
Standard input is empty
stdout
1: (220, 284)
2: (1184, 1210)
3: (2620, 2924)
4: (5020, 5564)
5: (6232, 6368)
6: (10744, 10856)
7: (12285, 14595)
8: (17296, 18416)
9: (63020, 76084)
10: (66928, 66992)
11: (67095, 71145)
12: (69615, 87633)
13: (79750, 88730)
14: (100485, 124155)