![]() |
VOOZH | about |
Given a number n, the task is to print its Aliquot Sequence. Aliquot Sequence of a number starts with itself, remaining terms of the sequence are sum of proper divisors of immediate previous term. For example, Aliquot Sequence for 10 is 10, 8, 7, 1, 0. The sequence may repeat. For example, for 6, we have an infinite sequence of all 6s. In such cases we print the repeating number and stop. Examples:
Input: n = 10 Output: 10 8 7 1 0 Sum of proper divisors of 10 is 5 + 2 + 1 = 8. Sum of proper divisors of 8 is 4 + 2 + 1 = 7. Sum of proper divisors of 7 is 1 Sum of proper divisors of 1 is 0 Note that there is no proper divisor of 1. Input : n = 6 Output : 6 Repeats with 6 Input : n = 12 Output : 12 16 15 9 4 3 1 0
Important Points:
The solution mainly lies in the calculation of sum of all the proper divisors of the previous term.
We can generate the sequence by first printing the number n and then calculating the next terms using sum of proper divisors. When we compute next term, we check if we have already seen this term or not. If the term appears again, we have repeating sequence. We print the same and break the loop.
Output:
12 16 15 9 4 3 1 0