P0003. Friend numbers

A pair of numbers are called friends if they are different and the sum of all the positive divisors of each number (included 1, but not included the own number) is equal to the other number.

For instance, 2620 and 2924 are friends: the divisors of 2620 (not included 2620) are 1, 2, 4, 5, 10, 20, 131, 262, 524, 655 and 1310, which sum is 2924. The divisors of 2924 (not included 2924) are 1, 2, 4, 17, 34, 43, 68, 86, 172, 731 and 1462, which sum is 2620.

Your task is to write a program that reads a sequence of pairs of natural numbers greater or equal than 2, and prints the friend pairs.

Your program must include two functions:

\begin{verbatim}
    int sum_divisors(int a);
\end{verbatim}

that returns the sum of the positive divisors of the natural number |a|2\mbox{|a|} \ge 2 that are strictly less than |a|, and

\begin{verbatim}
    bool are_friends(int a, int b);
\end{verbatim}

that indicates if two natural numbers |a|2\mbox{|a|} \ge 2 and |b|2\mbox{|b|} \ge 2 are friends.

Input

The input is a sequence a1,b1,a2,b2,,an,bna_1, b_1, a_2, b_2, \dots, a_n, b_n of nn pairs of natural numbers greater or equal than 2, with n0n \ge 0.

Output

Your program must print one line, which contains each pair of numbers aibia_i~b_i that are friends (including repetitions if necessary). The pairs must be written separated by commas and in brackets, following the format of the instances.

Problem information

Author: Unknown
Translator: Carlos Molina

Generation: 2026-01-25T11:19:07.952Z

© Jutge.org, 2006–2026.
https://jutge.org