I am trying to write a function which locates amicable pairs withina given range
ID: 3608967 • Letter: I
Question
I am trying to write a function which locates amicable pairs withina given range. An amicable number is one such that the sum of itsfactors equals a number whose sum of factors equal the originalnumber. For example, the sum of the factors of 220 is 284,and the sum of the factors of 284 is 220.This is the function description:
bool isAmicable(int start, int end, int number);
// REQUIRES: num is a positive integer value
// MODIFIES: nothing
// EFFECTS: returns true and prints out a messageif
// number is part of an amicable pair and
// number is the smaller of the two values.
// and within the range of start-end inclusive
// Returns false otherwise.
and this is the code I have written for the function: (I alreadywrote the function sumFactors which calculates the sum of thefactors of a number)
bool isAmicable(int start, int end, int number)
{
int a = number - 1;
int b = number + 1;
while(start <= a)
{
if((sumFactors(number) ==start) && (sumFactors(start) == number))
{
cout<< number << " and " << start << " areamicable" << endl;
returntrue;
}
start++;
while(b <= end)
{
if((sumFactors(number) == b) && (sumFactors(b) ==number))
{
cout << number << " and " << b<< " are amicable" << endl;
return true;
}
b++;
}
}
}
When I try to test my function, and I test for a number that has anamicable pair within the range, it works. In other words, ifI test
cout << isAmicable(210, 290, 220) << endl;
it correctly prints:
220 and 284 are amicable
1
the 1 just means the function returned true, which is supposed tohappen.
However, when I test for a number that does not have an amicablepair within the range, it prints out the number I tested for, whenit should just be printing out a 0, indicating the functionreturned false. In other words, when I test
cout << isAmicable(0, 10, 7) << endl;
it prints 7.
Please help me realize an error and help me correct my code. I know I need to put a return false; somewhere, but wherever I tryto put it, when I run my tests, every test returns false.
Explanation / Answer
#include<iostream.h>
bool isAmicable(int start, int end, int number);
// REQUIRES: num is a positive integer value
// MODIFIES: nothing
// EFFECTS: returns true and prints out a messageif
// number is part of an amicable pair and
// number is the smaller of the two values.
// and within the range of start-end inclusive
// Returns false otherwise.
intsumFactors(int);
using namespace std;
int main()
{int num1,num2;
cout<<isAmicable(210,290,220)<<endl;
cout << isAmicable(1100, 1500, 1210) << endl;
cout << isAmicable(0, 10, 7) << endl;
system("pause");
return 0;
}
int sumFactors(int a)
{int i,sum=1;
for(i=2;i<=a/2;i++)
if(a%i==0)
sum+=i;
return sum;
}
bool isAmicable(int start, int end, int number)
{int i;
for(i=start;i<=end;i++)
if((sumFactors(number) == i)&& (sumFactors(i) == number))
{cout<<number<<" and "<<i<<" areamicable ";
if(number<i)
{cout<<number <<" is the smaller ";
return true;
}
else
{cout<<number <<" is the larger ";
return false;
}
}
cout<<number<<" is not amicable with any numberin the given range ";
return false;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.