C# Amicable Number Program

Beginner
6 min read
Updated: Oct 2026
2 programs
Live preview

What Is an Amicable Number?

Two different positive integers a and b form an amicable pair when each equals the other’s proper-divisor sum (aliquot sum), written s(n).

Remember
s(n) = sum of proper divisors of n
a ≠ b
s(a) = b  and  s(b) = a  →  amicable pair

Proper divisors = positive divisors of n, except n itself
Example: s(220) = 284, s(284) = 220 → amicable
ConceptConditionExample
Amicable pairs(a)=b, s(b)=a, a≠b220 & 284
Perfects(n) = n6, 28
Abundants(n) > n12 (not a pair)

Well-known amicable pairs

Golden tests for interviews and unit checks — start with 220 and 284.

List
(220, 284), (1184, 1210), (2620, 2924), (5020, 5564)

How to Check an Amicable Pair in C#

Four clear steps — share one proper-divisor helper for both numbers.

  1. Reject if a < 2, b < 2, or a == b.
  2. Compute s(a) and s(b) (sum proper divisors).
  3. Require s(a) == b and s(b) == a.
  4. Optionally print both sums so the two-way link is visible.
MethodIdeaTimeBest for
Basic loopSum divisors from 1 to n / 2O(a + b)Interviews, small values
Divisor pairsLoop to √n; add i and n / i (skip n)O(√a + √b)Larger inputs, optimizations

Pseudocode

Pseudocode
function SumProperDivisors(n):
    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 < 2 or b < 2 or a == b:
        return false
    return SumProperDivisors(a) == b and SumProperDivisors(b) == a

Live Preview

Enter two positive integers to see s(a), s(b), and the amicable verdict.

Whole numbers ≥ 1 (capped at 999999). Order does not matter for a valid pair.

Result
a = 220, b = 284
s(220) = 284
s(284) = 220
a ≠ b: true
s(a) == b: true
s(b) == a: true
Verdict: AMICABLE PAIR

Worked Walkthrough — 220 & 284

Add the proper divisors of each number, then check both directions.

Calculation
220: 1 + 2 + 4 + 5 + 10 + 11 + 20 + 22 + 44 + 55 + 110 = 284
284: 1 + 2 + 4 + 71 + 142 = 220

s(220) = 284 and s(284) = 220
Result: 220 and 284 are an amicable pair

C# Programs

Start with the readable O(a + b) solution. Then use divisor pairs for an O(√a + √b) optimization.

Example 1 — Check One Pair

Shared SumProperDivisors helper, then the two-way amicable test.

C#
using System;

class Program
{
    static int SumProperDivisors(int n)
    {
        if (n <= 1)
        {
            return 0;
        }

        int sum = 0;
        for (int i = 1; i <= n / 2; i++)
        {
            if (n % i == 0)
            {
                sum += i;
            }
        }
        return sum;
    }

    static bool AreAmicable(int a, int b)
    {
        if (a < 2 || b < 2 || a == b)
        {
            return false;
        }

        return SumProperDivisors(a) == b && SumProperDivisors(b) == a;
    }

    static void Main()
    {
        int a = 220;
        int b = 284;

        if (AreAmicable(a, b))
        {
            Console.WriteLine(a + " and " + b + " are amicable numbers.");
        }
        else
        {
            Console.WriteLine(a + " and " + b + " are not amicable numbers.");
        }
    }
}

How it works: sum the proper divisors of both numbers. Return true only when the sums point to each other and the inputs are different.

Example 2 — O(√n) with Divisor Pairs

Same amicable test; faster sum using divisor pairs up to √n.

C#
using System;

class Program
{
    static int SumProperDivisorsFast(int n)
    {
        if (n <= 1)
        {
            return 0;
        }

        int sum = 1; // 1 is always a proper divisor for n > 1

        for (int i = 2; i <= n / i; i++)
        {
            if (n % i == 0)
            {
                sum += i;
                int partner = n / i;
                if (partner != i && partner != n)
                {
                    sum += partner;
                }
            }
        }

        return sum;
    }

    static bool AreAmicableFast(int a, int b)
    {
        if (a < 2 || b < 2 || a == b)
        {
            return false;
        }

        return SumProperDivisorsFast(a) == b
            && SumProperDivisorsFast(b) == a;
    }

    static void Main()
    {
        Console.WriteLine(AreAmicableFast(220, 284));   // True
        Console.WriteLine(AreAmicableFast(1184, 1210)); // True
        Console.WriteLine(AreAmicableFast(6, 6));       // False (perfect)
    }
}

How it works: each divisor i has a partner n / i. Add both once and stop at √n. Using i <= n / i avoids integer overflow.

Edge Cases & Pitfalls

Check these before calling the solution done.

a == b

Not a pair

Equal inputs are never amicable — that path is for perfect numbers.

One way

Check both

s(a) == b alone is not enough; also require s(b) == a.

Include n?

Never add n

Including n in the sum breaks every amicable and perfect check.

Squares

No double-count

In pair mode, add the square root only once when i * i == n.

Time and Space Complexity

ProgramTimeExtra space
Basic loops to n/2O(a + b)O(1)
Divisor pairs up to √nO(√a + √b)O(1)

Key Takeaways

  • Definition: s(a)=b, s(b)=a, and a≠b.
  • Helper first: one proper-divisor sum function powers the whole solution.
  • Two loops: basic 1 … n/2, or pairs up to √n.
  • Test set: (220, 284) true; (6, 6) false; (12, 18) false.

One line: compute both proper-divisor sums; if each equals the other number and they differ, the pair is amicable.

Frequently Asked Questions

Two different positive integers are amicable when each number equals the sum of the other number's proper divisors. The smallest pair is 220 and 284.
Create a helper that sums proper divisors. The pair is amicable when s(a) equals b, s(b) equals a, and a does not equal b.
If a number equals its own proper-divisor sum, it is a perfect number. An amicable pair must contain two different numbers.
Amicable compares two numbers whose proper-divisor sums point to each other. Abundant checks one number whose proper-divisor sum is greater than itself.
The basic method is O(a + b). The divisor-pair method is O(√a + √b). Both use O(1) extra space.

Next: Armstrong Number

Numbers equal to the sum of their own digits each raised to a power.

Armstrong number tutorial →

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.

12 people found this page helpful