Skip to content

Amicable numbers

Java sum of divisors

Flowchart (ISO 5807)

YesNoStartInput ns = 0i = 1, n − 1, 1n mod i = 0s = s + iReturn sEndFigure 1 — Amicable.sumDivisorsStartInput a, bReturn sumDivisors(a) = b and sumDivisors(b) = aEndFigure 2 — Amicable.areAmicable

Source code

Java
class Amicable {
    static int sumDivisors(int n) {
        int s = 0;
        for (int i = 1; i < n; i++) {
            if (n % i == 0) {
                s += i;
            }
        }
        return s;
    }

    static boolean areAmicable(int a, int b) {
        return sumDivisors(a) == b && sumDivisors(b) == a;
    }
}

The editor opens with this code — change it and the chart redraws.

Language
Java
Constructs
sum of divisors
Topic
Algorithms
Formats
Word, Visio, draw.io, PDF, SVG

Related examples