Amicable numbers in a range...

Code Guidelines and Requirements

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




This is what I have, and I'm stuck. Any help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
using namespace 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...?
        return true;
    }
    else
        return false;
}


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";
                return true;
            }
        }
    }
    cout << "No amicable values found in range: " << start << " - " << end << endl;
    return false;
}
	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;
}
can you please explain a bit more on how the amicable numbers get determined ? A small example, then I can try if I can fix your problem.
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.
Change your isAmicable function to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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)
	{
		return true;
	}
	else
	{
		cout << "No amicable values found in range: " << start << " - " << end << endl;
		return false;
	}
}


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.
Last edited on
Topic archived. No new replies allowed.