Aliquot Sequence in Java

In math, an aliquot sequence is like a number chain. In this chain, each number is made by adding up the smaller numbers that can divide into the previous number, but not including the number itself. These smaller numbers are called proper divisors.

Example 1:

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.

Example 2:

Input: n = 12

Output: 12 16 15 9 4 3 1 0

Sum of proper divisors of 12 is 6 + 4 + 3 + 2 + 1 = 16.

Sum of proper divisors of 16 is 8 + 4 + 2 + 1 = 15.

Sum of proper divisors of 15 is 9.

Sum of proper divisors of 9 is 4 + 3 + 1 = 8.

Sum of proper divisors of 4 is 2 + 1 = 3.

Sum of proper divisors of 3 is 1.

Sum of proper divisors of 1 is 0.

Example 3:

Input: n = 18

Output: 18 21 11 1 0

Sum of proper divisors of 18 is 9 + 6 + 3 + 2 + 1 = 21.

Sum of proper divisors of 21 is 11.

Sum of proper divisors of 11 is 1.

Sum of proper divisors of 1 is 0.

Example 4:

Input: n = 28

Output: 28 28 28 28 ... (repeats indefinitely)

Note: The Aliquot sequence for a perfect number (like 28) is a repeating pattern where the number itself is repeated indefinitely.

Approach: Brute Force Approach

The brute force approach used in the given code starts with a specific number and calculates the sum of its proper divisors. It then adds these sums as terms to the sequence. This process is repeated for a specified length, ensuring that the number 1 is not repeated in the sequence. While this method is straightforward, it needs to be optimized for larger numbers. There might be more efficient solutions for generating aliquot sequences, especially when dealing with larger initial numbers.

Algorithm:

Step 1: The algorithm begins by importing the necessary libraries, namely ArrayList and List, to work with lists of integers.

Step 2: Two variables, startingNumber and sequenceLength, are set to 12 and 10, respectively, determining the initial number for the sequence and the desired length of the sequence.

Step 3: The generateAliquotSequence Method is called, passing startingNumber and sequenceLength as arguments to generate the Aliquot Sequence.

Step 4: Within the generateAliquotSequence method, an empty list called aliquotSequence is initialized. The startingNumber is added to aliquotSequence, and currentNumber is set to startingNumber. A loop is initiated from 1 to sequenceLength - 1.

  1. Calculate nextNumber by calling sumOfProperDivisors(currentNumber).
  2. If nextNumber is equal to 1, add 1 to aliquotSequence and break the
  3. Add nextNumber to aliquotSequence.
  4. Update currentNumber with nextNumber.

Step 5: Add 0 to aliquotSequence to mark the end of the sequence.

  • Print "Aliquot Sequence:".
  • Iterate through aliquotSequence and print each term.

Step 6: Another list called sequence is initialized, and startingNumber is added to it. The currentNumber is set to startingNumber.

Step 7: A loop is started from 1 to sequenceLength - 1. During each iteration, the nextNumber is calculated similarly to the previous step. When the `nextNumber` in the sequence calculation is 1, it is added to the sequence to prevent repeating occurrences. The loop is then terminated. If `nextNumber` is not 1, it is added to the sequence, and `currentNumber` is updated to this value for the next iteration.

Step 8: Add 0 to the sequence to end with zero. Return sequence as the Aliquot Sequence.

Step 9: The sumOfProperDivisors method is defined. It takes an integer number as input and initializes the sum to 1. A loop runs from 2 to the square root of a number.

  • When the number is divisible by i, add i to the sum.
  • If i is not equal to number/i, add number / i to sum.
  • Return sum as the sum of proper divisors.

Step 10: The program ends, having generated the Aliquot Sequence and calculated the sum of proper divisors based on the provided input values.

Implementation:

Filename: AliquotSequence.java

Output:

Aliquot Sequence:
12 16 15 9 4 3 1 0

Time Complexity: The Loop runs for sequenceLength times.

Inside the Loop, the sumOfProperDivisors function is called, which has a time complexity of approximately O(sqrt(n)), where n is the current number in the sequence.

Therefore, the overall time complexity of generating the Aliquot Sequence is approximately O(sequenceLength * sqrt(n)).

Space Complexity: The Loop in sumOfProperDivisors runs until the square root of the current number. O(sequenceLength) for storing the Aliquot Sequence in an ArrayList.






Latest Courses