Check Amicable Number in Java

Beginner
⏱️ 13 min read
📚 Updated: May 2026
🎯 2 Code Examples
Number theory

What you’ll learn

  • The definition of an amicable pair using the sum of proper divisors s(n), and why a != b matters.
  • 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, and System.out.println.
  • for loops, integer arithmetic, and the modulo operator % to test divisibility.
  • The idea of proper divisors of n: positive divisors strictly less than n.

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.

Amicable a != b, s(a) = b, s(b) = a
Perfect s(n) = n
Not amicable fails any check above

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.

Formal condition

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.

Sketch for a = 220
  1. Proper divisors of 220 include 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110.
  2. Their sum is s(220) = 284.
  3. Proper divisors of 284 include 1, 2, 4, 71, 142, which sum to s(284) = 220.
  4. 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.

220 & 284 Amicable
s(220)
284
s(284)
220
Verdict
Both directions match and 220 != 284 - amicable.
6 & 6 Not amicable
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.
10 & 9 Not amicable
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.

  1. Try 220 and 284, or experiment with your own pair.
  2. Press Run check (or Enter in either field).
  3. Read whether the pair is amicable and which condition failed if not.

Use whole numbers a, b >= 1. Values above 999 999 are blocked so the tab stays responsive.

Live result
Press “Run check” to see s(a), s(b), and the amicable verdict.

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

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) = a
1

Check 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.

java
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.");
        }
    }
}
2

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.

java
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

Two different positive integers a and b are amicable if the sum of proper divisors of a equals b, and the sum of proper divisors of b equals a. The smallest such pair is (220, 284).
No. The definition requires two distinct numbers. If a equals b, you would only need s(a)=a, which describes a perfect number, not an amicable pair.
There are no proper divisors of 1 in the usual convention, so s(1)=0. Returning 0 also avoids wrong loops for 0 or negative input.
All three notions use s(n), the sum of proper divisors. Perfect means s(n)=n. Abundant means s(n)>n. Amicable links two different numbers by s(a)=b and s(b)=a.
If each sum uses sqrt factorization, one pair check is O(sqrt(a)+sqrt(b)). A naive loop to n/2 per value is O(a+b).
Start with the clear loop to n/2 if you want speed to code. Mention the sqrt divisor-pair method when asked about large inputs or optimization.

🔄 Input / output examples

Assume literals a and b as in the sample programs. For interactive input you can read two integers using Scanner.

abTypical line printed
220284220 and 284 are amicable numbers.
284220284 and 220 are amicable numbers.
10910 and 9 are not amicable numbers.
666 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.

Equality

a == b

Reject this case for a standard amicable-pair check. Otherwise a perfect number would be flagged incorrectly.

One-way

Only checking s(a) == b

You also need s(b) == a. One equality alone is not enough.

Sqrt loop

Perfect squares

When i * i == num, add i only once when pairing with num / i.

Bounds

s(1) and small values

Return 0 for n <= 1 so pair checks involving 1 do not mis-sum.

Overflow

Wide sums

For large n, s(n) can exceed 32-bit range; use long when needed.

⏱️ Time and space complexity

TaskTimeExtra space
s(n) naive (1 .. n/2)O(n)O(1)
s(n) sqrt pairingO(sqrt(n))O(1)
areAmicable(a,b) (two sqrt sums)O(sqrt(a) + sqrt(b))O(1)
areAmicable with two naive sumsO(a + b)O(1)

Both sample programs use only a handful of variables: auxiliary space is constant.

Summary

  • Definition: distinct a, b with s(a)=b and s(b)=a. Classic example: 220 and 284.
  • Code: implement s(n) (naive or sqrt), then combine with a != b and two equalities.
  • Watch-outs: both directions, exclude a=b, square-root double counting, and integer overflow on large inputs.
Did you know?

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful