How to resolve the algorithm Esthetic numbers step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Esthetic numbers step by step in the Java programming language

Table of Contents

Problem Statement

An esthetic number is a positive integer where every adjacent digit differs from its neighbour by 1.

These examples are nominally in base 10 but the concept extends easily to numbers in other bases. Traditionally, single digit numbers are included in esthetic numbers; zero may or may not be. For our purposes, for this task, do not include zero (0) as an esthetic number. Do not include numbers with leading zeros. Esthetic numbers are also sometimes referred to as stepping numbers.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Esthetic numbers step by step in the Java programming language

The provided Java code deals with the concept of esthetic numbers, which are numbers whose digits differ by exactly 1 or are consecutive when expressed in a given base. The code explores these numbers in various bases and provides examples.

Key Features:

1. isEsthetic Method:

  • Checks if a given number n is esthetic in a given base b.
  • It ensures that consecutive digits differ by exactly 1, and special handling is done for the first and last digits.

2. listEsths Method:

  • Generates a list of esthetic numbers within specified ranges for a given base.
  • Utilizes a recursive tri-consumer (RecTriConsumer) to traverse and explore the space of esthetic numbers.

3. Main Method:

  • The program's entry point.
  • It investigates esthetic numbers in bases 2 to 16, displaying 4th to 6th esthetic numbers in each base.
  • It also provides examples and showcases the listEsths method for various number ranges and bases.

4. RecTriConsumer Interface:

  • A functional interface that accepts a tri-consumer and three arguments.
  • Used in the listEsths method for recursive enumeration of esthetic numbers.

Example Usage:

The code showcases the functionality of esthetic numbers in various scenarios, such as:

  • Line 48-51: Finds and prints the 4th to 6th esthetic numbers in bases 2 to 16.
  • Line 63-74: Lists all esthetic numbers between 1000-1010 and 9999-9898 in base 10, with formatting.
  • Line 76-85: Similar to above, but for larger ranges in base 10.
  • Line 87-96: Lists esthetic numbers within specific ranges, such as 1e8-13e7 and 1e11-13e10, in base 10.

In summary, the code demonstrates the concept of esthetic numbers, providing methods for checking their validity and generating lists of them. It also showcases the iterative and recursive exploration of these numbers through the listEsths method and RecTriConsumer interface.

Source code in the java programming language

import java.util.ArrayList;
import java.util.stream.IntStream;
import java.util.stream.LongStream;

public class EstheticNumbers {
    interface RecTriConsumer<A, B, C> {
        void accept(RecTriConsumer<A, B, C> f, A a, B b, C c);
    }

    private static boolean isEsthetic(long n, long b) {
        if (n == 0) {
            return false;
        }
        var i = n % b;
        var n2 = n / b;
        while (n2 > 0) {
            var j = n2 % b;
            if (Math.abs(i - j) != 1) {
                return false;
            }
            n2 /= b;
            i = j;
        }
        return true;
    }

    private static void listEsths(long n, long n2, long m, long m2, int perLine, boolean all) {
        var esths = new ArrayList<Long>();
        var dfs = new RecTriConsumer<Long, Long, Long>() {
            public void accept(Long n, Long m, Long i) {
                accept(this, n, m, i);
            }

            @Override
            public void accept(RecTriConsumer<Long, Long, Long> f, Long n, Long m, Long i) {
                if (n <= i && i <= m) {
                    esths.add(i);
                }
                if (i == 0 || i > m) {
                    return;
                }
                var d = i % 10;
                var i1 = i * 10 + d - 1;
                var i2 = i1 + 2;
                if (d == 0) {
                    f.accept(f, n, m, i2);
                } else if (d == 9) {
                    f.accept(f, n, m, i1);
                } else {
                    f.accept(f, n, m, i1);
                    f.accept(f, n, m, i2);
                }
            }
        };

        LongStream.range(0, 10).forEach(i -> dfs.accept(n2, m2, i));

        var le = esths.size();
        System.out.printf("Base 10: %d esthetic numbers between %d and %d:%n", le, n, m);
        if (all) {
            for (int i = 0; i < esths.size(); i++) {
                System.out.printf("%d ", esths.get(i));
                if ((i + 1) % perLine == 0) {
                    System.out.println();
                }
            }
        } else {
            for (int i = 0; i < perLine; i++) {
                System.out.printf("%d ", esths.get(i));
            }
            System.out.println();
            System.out.println("............");
            for (int i = le - perLine; i < le; i++) {
                System.out.printf("%d ", esths.get(i));
            }
        }
        System.out.println();
        System.out.println();
    }

    public static void main(String[] args) {
        IntStream.rangeClosed(2, 16).forEach(b -> {
            System.out.printf("Base %d: %dth to %dth esthetic numbers:%n", b, 4 * b, 6 * b);
            var n = 1L;
            var c = 0L;
            while (c < 6 * b) {
                if (isEsthetic(n, b)) {
                    c++;
                    if (c >= 4 * b) {
                        System.out.printf("%s ", Long.toString(n, b));
                    }
                }
                n++;
            }
            System.out.println();
        });
        System.out.println();

        // the following all use the obvious range limitations for the numbers in question
        listEsths(1000, 1010, 9999, 9898, 16, true);
        listEsths((long) 1e8, 101_010_101, 13 * (long) 1e7, 123_456_789, 9, true);
        listEsths((long) 1e11, 101_010_101_010L, 13 * (long) 1e10, 123_456_789_898L, 7, false);
        listEsths((long) 1e14, 101_010_101_010_101L, 13 * (long) 1e13, 123_456_789_898_989L, 5, false);
        listEsths((long) 1e17, 101_010_101_010_101_010L, 13 * (long) 1e16, 123_456_789_898_989_898L, 4, false);
    }
}


  

You may also check:How to resolve the algorithm Assertions step by step in the Raku programming language
You may also check:How to resolve the algorithm Quad-power prime seeds step by step in the J programming language
You may also check:How to resolve the algorithm Zeckendorf number representation step by step in the Clojure programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the Lua programming language
You may also check:How to resolve the algorithm Sum of squares step by step in the Asymptote programming language