How to resolve the algorithm Aliquot sequence classifications step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Aliquot sequence classifications step by step in the Java programming language

Table of Contents

Problem Statement

An aliquot sequence of a positive integer K is defined recursively as the first member being K and subsequent members being the sum of the Proper divisors of the previous term.

Show all output on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Aliquot sequence classifications step by step in the Java programming language

The AliquotSequenceClassifications class is designed to classify aliquot sequences for given numbers. Aliquot sequences are sequences of numbers where each subsequent number is the sum of the proper divisors of the previous number. Proper divisors are the positive divisors of a number, excluding the number itself. This classifier categorizes sequences as "Perfect," "Amicable," "Sociable," "Aspiring," "Cyclic," "Terminating," or "Non-terminating."

Key Methods:

  • properDivsSum(long n): Calculates the sum of proper divisors for a given number n.
  • aliquot(long n, int maxLen, long maxTerm): This method performs the aliquot sequence classification by iteratively computing the next number in the sequence and classifying it based on its behavior.

Classification Logic:

The aliquot sequence classification logic is as follows:

  1. Perfect: If the starting number and the sum of its proper divisors are numerically equal.
  2. Amicable: If the sequence consists of two numbers where the sum of proper divisors of each number is the other number.
  3. Sociable of length N: If the sequence consists of N numbers where the sum of proper divisors of the last number returns to the starting number.
  4. Aspiring: If the sequence starts and ends with the same number.
  5. Cyclic back to N: If the sequence eventually reaches a previously visited number N.
  6. Terminating: If the sum of proper divisors of any number in the sequence results in 0.
  7. Non-terminating: If none of the above conditions are met within the specified limits.

Usage:

The main method demonstrates the classification by providing sample numbers and printing their classifications. The default limits for sequence length and maximum term are set to 16 and 2^47, respectively.

Additional Notes:

  • The program initializes an empty list s to store the aliquot sequence and initializes newN to the starting number n.
  • The classification report function report(String msg, List<Long> result) prints the classification message with the resulting sequence.
  • The program tests a range of numbers from 1 to 10 and the specified array arr to showcase the classifications.

Source code in the java programming language

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.LongStream;

public class AliquotSequenceClassifications {

    private static Long properDivsSum(long n) {
        return LongStream.rangeClosed(1, (n + 1) / 2).filter(i -> n % i == 0 && n != i).sum();
    }

    static boolean aliquot(long n, int maxLen, long maxTerm) {
        List<Long> s = new ArrayList<>(maxLen);
        s.add(n);
        long newN = n;

        while (s.size() <= maxLen && newN < maxTerm) {

            newN = properDivsSum(s.get(s.size() - 1));

            if (s.contains(newN)) {

                if (s.get(0) == newN) {

                    switch (s.size()) {
                        case 1:
                            return report("Perfect", s);
                        case 2:
                            return report("Amicable", s);
                        default:
                            return report("Sociable of length " + s.size(), s);
                    }

                } else if (s.get(s.size() - 1) == newN) {
                    return report("Aspiring", s);

                } else
                    return report("Cyclic back to " + newN, s);

            } else {
                s.add(newN);
                if (newN == 0)
                    return report("Terminating", s);
            }
        }

        return report("Non-terminating", s);
    }

    static boolean report(String msg, List<Long> result) {
        System.out.println(msg + ": " + result);
        return false;
    }

    public static void main(String[] args) {
        long[] arr = {
                11, 12, 28, 496, 220, 1184, 12496, 1264460,
                790, 909, 562, 1064, 1488};

        LongStream.rangeClosed(1, 10).forEach(n -> aliquot(n, 16, 1L << 47));
        System.out.println();
        Arrays.stream(arr).forEach(n -> aliquot(n, 16, 1L << 47));
    }
}


  

You may also check:How to resolve the algorithm Multi-dimensional array step by step in the jq programming language
You may also check:How to resolve the algorithm Count in octal step by step in the BASIC programming language
You may also check:How to resolve the algorithm Integer sequence step by step in the NewLISP programming language
You may also check:How to resolve the algorithm Matrix chain multiplication step by step in the Perl programming language
You may also check:How to resolve the algorithm Terminal control/Display an extended character step by step in the Lasso programming language