CODE
#include <iostream>
using namespace std;
void sum( int n );
int main()
{
int a, b;
cout << "Enter 2 positive integers a <= b: " << flush;
cin >> a >> b;
int c = b;
int d = a;
int tmp;
for( int n = a; n <= b; n++ )
{
tmp = n - sum(n);
if( tmp < 0 )
tmp = -tmp;
if( tmp <= c )
tmp = c;
d = n;
}
cout << "The distance is minimal for " << d << " and is equal to " << min << endl;
system("pause");
return 0;
}
void sum( int &n )
{
int s = 1;
// if d is a divisor of n so is n/d avoid to search till n
for( int d = 2; d*d <= n; d++ )
if( n%d == 0 )
s += d + n/d;
return s;
}
I am not sure what i have done wrong. The program is meant to scan an interval [a, b] and return the smallest number that is the closest to its sum of divisors and it does not compile.