Amicable Numbers
Time limit1sMemory limit128 MB
For each N, print all pairs of distinct numbers up to N where each equals the sum of the other's proper divisors.
- Level
Medium4 of 10
- Topics
- Number theory, Brute force, Implementation
- Solved
- No attempts yet
Problem
The two natural numbers 220 and 284 are called amicable numbers because they are different from each other and, for each one, the sum of its proper divisors (its divisors excluding the number itself) equals the other number. That is, the sum of the proper divisors of 220,
equals 284, and the sum of the proper divisors of 284,
equals 220.
Given a natural number, write a program that finds every amicable pair whose two members both do not exceed the given value.
Input
The input consists of several lines, each containing a single integer (). The input ends with a line containing 0, which must not be processed.
Output
For each input value , print a header line reading Amicable numbers between 1 and N (with N replaced by the actual value). Then print every distinct amicable pair both of whose members lie between and , one pair per line, as the two numbers separated by a single space with the smaller number first. List the pairs in ascending order of their smaller member. If no such pair exists, print None instead. Separate the outputs for consecutive input values with a single blank line. A number cannot form an amicable pair with itself.