fork download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. int divisorsSum( int x )
  5. {
  6. int result = 1;
  7.  
  8. int max = sqrt(x) + 1;
  9. for ( int d = 2; d <= max; ++d )
  10. {
  11. if ( x % d == 0 )
  12. {
  13. result += d + x/d;
  14. }
  15. }
  16.  
  17. return result;
  18. }
  19.  
  20. void findNFriends( int n )
  21. {
  22. int findedCount = 0;
  23. for ( int f = 0; ; ++f )
  24. {
  25. int s = divisorsSum( f );
  26. if ( (f < s) && (f == divisorsSum( s )))
  27. {
  28. ++findedCount;
  29. std::cout << findedCount << ": (" << f << ", " << s << ")" << std::endl;
  30.  
  31. if ( findedCount >= n ) break;
  32. }
  33. }
  34. }
  35.  
  36. int main() {
  37. findNFriends(26);
  38. return 0;
  39. }
Success #stdin #stdout 3.74s 2680KB
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)
15: (122265, 139815)
16: (122368, 123152)
17: (141664, 153176)
18: (142310, 168730)
19: (171856, 176336)
20: (176272, 180848)
21: (185368, 203432)
22: (196724, 202444)
23: (280540, 365084)
24: (308620, 389924)
25: (319550, 430402)
26: (356408, 399592)