- s(220)
- 284
- s(284)
- 220
- Verdict
- Both directions match and
220 != 284- amicable.
Check Amicable Number in Java
What you’ll learn
- The definition of an amicable pair using the sum of proper divisors
s(n), and whya != bmatters. - A compact mathematical formulation and classic examples such as 220 and 284.
- A clear algorithm, pseudocode, and two complete Java programs (sqrt pairing and naive scan).
- Time and space complexity, common edge cases, and interview-style optimizations.
Overview
This page is an interview-style walkthrough: given two positive integers a and b, decide whether they form an amicable pair - each is the sum of the proper divisors of the other, and the two values are different.
Pair logic
Compute s(a) and s(b), then check s(a)=b, s(b)=a, and a != b. Order of arguments does not matter.
Live preview
Try 220 and 284 (or your own values) in the browser before compiling Java.
Tables and rigor
I/O examples, edge cases, and complexity notes cover the usual follow-ups.
Prerequisites
You should already be able to read and write small Java programs before following the examples below.
- Comfortable with Java syntax:
class Main,public static void main(String[] args), methods, andSystem.out.println. forloops, integer arithmetic, and the modulo operator%to test divisibility.- The idea of proper divisors of
n: positive divisors strictly less thann.
What is an amicable pair?
Write s(n) for the sum of all proper divisors of n (positive divisors of n that are strictly less than n). Two different positive integers a and b are an amicable pair when s(a) = b and s(b) = a.
That is a symmetric condition: swapping a and b does not change the answer. If a = b, you would only need s(a) = a, which defines a perfect number, not an amicable pair.
Mathematical definition
Let s(n) denote the sum of proper divisors of n. Equivalently, if σ(n) is the sum of all positive divisors of n, then s(n) = σ(n) - n.
Positive integers a and b form an amicable pair if and only if a != b, s(a) = b, and s(b) = a.
Smallest pair: 220 and 284. One checks s(220) = 284 and s(284) = 220.
a = 220- Proper divisors of 220 include
1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110. - Their sum is
s(220) = 284. - Proper divisors of 284 include
1, 2, 4, 71, 142, which sum tos(284) = 220. - Since
220 != 284, the two numbers are amicable.
Intuition and examples
Think of s(n) as: "add all smaller divisors of n." An amicable pair is two different numbers that point at each other: the divisor-sum of the first lands on the second, and vice versa.
The cards contrast a true amicable pair with cases that fail one of the required checks.
- s(6)
- 1 + 2 + 3 = 6
- Pair rule
- Here
a = b; the definition excludes identical values. - Verdict
- 6 is perfect, not an amicable pair with itself.
- s(10)
- 1 + 2 + 5 = 8 (not 9)
- s(9)
- 1 + 3 = 4 (not 10)
- Verdict
- Neither equality holds - not amicable.
Takeaway: always verify both s(a)=b and s(b)=a, and reject a=b.
Live preview
Enter two positive integers a and b. The tool computes s(a) and s(b) by summing every proper divisor, then checks the amicable conditions. Everything runs in your browser.
- Try 220 and 284, or experiment with your own pair.
- Press Run check (or Enter in either field).
- Read whether the pair is amicable and which condition failed if not.
Algorithm
Goal: given positive integers a and b, decide whether they form an amicable pair.
Validate inputs
Reject non-positive values if your program must follow the number-theory definition.
Compute s(a) and s(b)
Sum every divisor d with 1 <= d < n and n % d == 0, or use the faster sqrt pairing method.
Enforce a != b
If a == b, return false for the standard definition.
Compare both directions
Return true iff s(a) == b and s(b) == a.
📜 Pseudocode
function properDivisorSum(n):
if n < 1:
return invalid
if n = 1:
return 0
sum ← 0
for i from 1 to floor(n / 2):
if n mod i = 0:
sum ← sum + i
return sum
function areAmicable(a, b):
if a = b:
return false
return properDivisorSum(a) = b and properDivisorSum(b) = aCheck one pair (sqrt sum, with explanation)
sumProperDivisors returns s(n) in O(sqrt(n)) time using divisor pairs. areAmicable rejects a == b and tests both directions.
public class Main {
// Sum of proper divisors s(n); by convention s(1) = 0
static int sumProperDivisors(int num) {
if (num <= 1) {
return 0;
}
int sum = 1; // 1 is a proper divisor for any num > 1
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
sum += i;
int pair = num / i;
if (pair != i) {
sum += pair;
}
}
}
return sum;
}
static boolean areAmicable(int a, int b) {
if (a == b) {
return false;
}
return sumProperDivisors(a) == b && sumProperDivisors(b) == a;
}
public static void main(String[] args) {
int a = 220;
int b = 284;
if (areAmicable(a, b)) {
System.out.println(a + " and " + b + " are amicable numbers.");
} else {
System.out.println(a + " and " + b + " are not amicable numbers.");
}
}
}Same pair check (naive divisor sum)
This version uses a loop to n/2 for each sum. It is O(n) per value but matches the pseudocode line-by-line and is easy to explain first in an interview.
public class Main {
static int sumProperDivisorsNaive(int num) {
if (num <= 1) {
return 0;
}
int sum = 0;
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) {
sum += i;
}
}
return sum;
}
static boolean areAmicableNaive(int a, int b) {
if (a == b) {
return false;
}
return sumProperDivisorsNaive(a) == b && sumProperDivisorsNaive(b) == a;
}
public static void main(String[] args) {
int a = 220;
int b = 284;
if (areAmicableNaive(a, b)) {
System.out.println(a + " and " + b + " are amicable numbers.");
} else {
System.out.println(a + " and " + b + " are not amicable numbers.");
}
}
}Optimization
Single pair. Use divisor pairing up to sqrt(n) for each s(a) and s(b) when inputs may be large.
Many pairs or a range. Precompute s(k) for all k in [1, N] with a sieve-like accumulation, then each pair test is O(1) lookups after O(N log N) setup.
Overflow. For larger constraints, switch sums to long so s(n) does not overflow int.
Interview: write the naive sum first if time is short, then mention the sqrt speedup.
❓ FAQ
🔄 Input / output examples
Assume literals a and b as in the sample programs. For interactive input you can read two integers using Scanner.
a | b | Typical line printed |
|---|---|---|
| 220 | 284 | 220 and 284 are amicable numbers. |
| 284 | 220 | 284 and 220 are amicable numbers. |
| 10 | 9 | 10 and 9 are not amicable numbers. |
| 6 | 6 | 6 and 6 are not amicable numbers. |
Edge cases and pitfalls
These mistakes do not change the definition of amicable numbers; they change whether your program matches it.
a == b
Reject this case for a standard amicable-pair check. Otherwise a perfect number would be flagged incorrectly.
Only checking s(a) == b
You also need s(b) == a. One equality alone is not enough.
Perfect squares
When i * i == num, add i only once when pairing with num / i.
s(1) and small values
Return 0 for n <= 1 so pair checks involving 1 do not mis-sum.
Wide sums
For large n, s(n) can exceed 32-bit range; use long when needed.
⏱️ Time and space complexity
| Task | Time | Extra space |
|---|---|---|
s(n) naive (1 .. n/2) | O(n) | O(1) |
s(n) sqrt pairing | O(sqrt(n)) | O(1) |
areAmicable(a,b) (two sqrt sums) | O(sqrt(a) + sqrt(b)) | O(1) |
areAmicable with two naive sums | O(a + b) | O(1) |
Both sample programs use only a handful of variables: auxiliary space is constant.
Summary
- Definition: distinct
a,bwiths(a)=bands(b)=a. Classic example: 220 and 284. - Code: implement
s(n)(naive or sqrt), then combine witha != band two equalities. - Watch-outs: both directions, exclude
a=b, square-root double counting, and integer overflow on large inputs.
The pair 220 and 284 is the smallest amicable pair: each number equals the sum of the proper divisors of the other. They appear in early manuscripts as symbols of friendship.
9 people found this page helpful
