Some numbers have a curious relationship with their own divisors. The number 6 equals the sum of its smaller divisors, 1 + 2 + 3. Such numbers fascinated ancient mathematicians, and they provide excellent practice in summing divisors with a loop.
Proper Divisors
Definition: Proper divisor. A positive divisor of a number other than the number itself.
The proper divisors of 12 are 1, 2, 3, 4 and 6. Their sum is 16. The sum of the proper divisors of n is the basis of both problems in this topic.
Summing the Proper Divisors
The sum is found with a counter-controlled loop. Every d from 1 to n / 2 is tried; if n % d == 0, then d is added to the sum. No proper divisor can exceed n / 2, so the loop need not go further.
sum = 0
FOR d = 1 TO n / 2
IF n MOD d = 0 THEN
sum = sum + d
ENDIF
ENDFOR
Perfect Numbers
Definition: Perfect number. A positive integer that is equal to the sum of its proper divisors.
The smallest perfect number is 6, since 1 + 2 + 3 = 6. The next is 28, since 1 + 2 + 4 + 7 + 14 = 28. The next two are 496 and 8128. Perfect numbers are rare. Table 3.4 classifies some numbers by comparing the divisor sum with the number itself.
Table 3.4: Sum of proper divisors of some numbers
| Number | Proper divisors | Sum | Perfect? |
|---|---|---|---|
| 6 | 1, 2, 3 | 6 | Yes |
| 10 | 1, 2, 5 | 8 | No |
| 12 | 1, 2, 3, 4, 6 | 16 | No |
| 28 | 1, 2, 4, 7, 14 | 28 | Yes |
Amicable Numbers
Definition: Amicable numbers. Two different positive integers such that the sum of the proper divisors of each is equal to the other.
The smallest amicable pair is 220 and 284.
- The proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110. Their sum is 284.
- The proper divisors of 284 are 1, 2, 4, 71 and 142. Their sum is 220.
Two conditions must both hold, and the two numbers must be different. A perfect number is not regarded as amicable with itself.
Method for Both Problems
Both tests rely on the same subproblem, the sum of proper divisors. This is decomposition at work.
- Perfect: compute the divisor sum of n and compare it with n.
- Amicable: compute the divisor sum of a and the divisor sum of b; the pair is amicable when the first equals b, the second equals a, and a differs from b.
Example 3.4: Testing an amicable pair
Problem: Read two positive integers and report whether they form an amicable pair.
Solution:
- Compute sumA, the sum of proper divisors of a.
- Compute sumB, the sum of proper divisors of b.
- The pair is amicable if sumA equals b, sumB equals a and a is not equal to b.
#include <stdio.h>
int main(void)
{
int a, b, d, sumA = 0, sumB = 0;
printf("Enter two positive integers: ");
scanf("%d %d", &a, &b);
for (d = 1; d <= a / 2; d++)
{
if (a % d == 0)
{
sumA = sumA + d;
}
}
for (d = 1; d <= b / 2; d++)
{
if (b % d == 0)
{
sumB = sumB + d;
}
}
printf("\nDivisor sum of %d = %d\n", a, sumA);
printf("Divisor sum of %d = %d\n", b, sumB);
if (sumA == b && sumB == a && a != b)
{
printf("They are amicable numbers\n");
}
else
{
printf("They are not amicable numbers\n");
}
return 0;
}
Input:
220 284
Output:
Enter two positive integers:
Divisor sum of 220 = 284
Divisor sum of 284 = 220
They are amicable numbers
Result: Each number equals the divisor sum of the other, so 220 and 284 are amicable. The two loops are identical apart from the variable names; in Unit IV such repeated work is placed in a function.
Common mistakes
- Including the number itself among its divisors when forming the sum.
- Checking only one of the two conditions for an amicable pair.
- Running the divisor loop up to n instead of n / 2, which then adds n itself.
Key takeaways
- A proper divisor of n is a divisor smaller than n.
- A perfect number equals the sum of its proper divisors; 6 and 28 are examples.
- In an amicable pair, each number equals the divisor sum of the other; 220 and 284 are the smallest pair.
- Both tests use the same subproblem: the sum of proper divisors.
Practice questions
- Find the sum of the proper divisors of 20.
- Is 496 a perfect number?
- Why does the divisor loop stop at n / 2?
Answers to the practice questions
- 1 + 2 + 4 + 5 + 10 = 22.
- Yes. 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248 = 496.
- No proper divisor of n can be greater than n / 2.
Sources
- Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit III
