Given two numbers, if the sum of the proper divisors of the first number is equal to the second
number and the sum of the proper divisors of the second number is equal to the first number,
the two are considered amicable numbers.
You are to write a program that:
• inputs a range of values to process
• finds any pairs of amicable numbers in that range and outputs them in ascending order.
• continues to run until you input a value that is out of range.
Sample Run:
Input the start of the range: 1
Input the end of the range: 300
220 and 284 are amicable numbers
In the range: 1 - 300
count
-----
1 pair of amicable numbers
#include <iostream>
usingnamespace std;
bool isAmicable(int, int);
int sumFactors(int);
int sumItselfFactors(int);
enum {PERFECT = 1, DEFICIENT, ABUNDANT, AMICABLE};
int main()
{
}
bool isAbundant(int number)
{
int i = sumItselfFactors(number);
if (i > (2*number)) //your equation for abundant numbers was off its factored-sum > 2n
{
//you had 'i' in the output...?
returntrue;
}
elsereturnfalse;
}
bool isAmicable(int start, int end)//you want to give it a range, but to check we need an actual number (third param
{
//220 and 284 are the smallest amicable numbers
for(int i = start; i <= end; i++)
{
for(int j = end; j >= start; j--)
{
if ((sumFactors(i)==j) && (sumFactors(j)==i) && (i != j)) //last conditional is important other wise it will return a same pair of numbers
{
cout << i << " and " << j << " are amicable\n";
returntrue;
}
}
}
cout << "No amicable values found in range: " << start << " - " << end << endl;
returnfalse;
}
isAmicable(start,end); //function will take care of our output for simplicity's sake, others just return a value
int sumFactors(int number) //this function is needed when we want all positive factors, but not the number itself
{
int factor = 0, sum = 0;
for (int i = 1; i < number; i++){ //only <
if (!(number % i)){
factor = i;
sum += factor;
}
}
return sum;
}
int sumItselfFactors(int number) //this function is needed when we want all positive factors, INCLUDING the number itself
{
int factor = 0, sum = 0;
for (int i = 1; i <= number; i++){ //difference is here <=
if (!(number % i)){
factor = i;
sum += factor;
}
}
return sum;
}
Amicable numbers are if the sum of the proper divisors of the first number is equal to the second number and the sum of the proper divisors of the second number is equal to the first number, then two are considered amicable numbers.
As an example, 220 and 284 are amicable numbers. The proper divisors of 220 – the 1st number
-- are:
1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110
When you sum these together you get 284 – the 2nd number. The proper divisors of 284 – the
2nd number -- are:
1, 2, 3, 71, and 142
If you add these up you get 220 – the 1st number.
bool isAmicable(int start, int end)//you want to give it a range, but to check we need an actual number (third param
{
bool amicable = false;
//220 and 284 are the smallest amicable numbers
for(int i = start; i <= end; i++)
{
for(int j = end; j >= start; j--)
{
if ((sumFactors(i)==j) && (sumFactors(j)==i) && (i != j)) //last conditional is important other wise it will return a same pair of numbers
{
cout << i << " and " << j << " are amicable\n";
//return true;
amicable = true;
break;
}
}
}
if(amicable)
{
returntrue;
}
else
{
cout << "No amicable values found in range: " << start << " - " << end << endl;
returnfalse;
}
}
You can tweak it a little so the same pairs in a different order are not outputted but now it will find all of the pairs from x to y.