#include <iostream> #include <math.h> int divisorsSum( int x ) { int result = 1; int sq= (int) sqrt(x)+1; for ( int d = 2; d <= sq; ++d ) { if ( x % d == 0 ) { result += d + x/d; } } return result; } void findNFriends( int n ) { int findedCount = 0; for ( int f = 0; ; ++f ) { int s = divisorsSum( f ); if ( (f < s) && (f == divisorsSum( s ))) { ++findedCount; std::cout << findedCount << ": (" << f << ", " << s << ")" << std::endl; if ( findedCount >= n ) break; } } } int main() { findNFriends(14); return 0; }
Standard input is empty
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)