How to resolve the algorithm Largest number divisible by its digits step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Largest number divisible by its digits step by step in the Java programming language

Table of Contents

Problem Statement

Find the largest base 10 integer whose digits are all different,   and   is evenly divisible by each of its individual digits.

These numbers are also known as   Lynch-Bell numbers,   numbers   n   such that the (base ten) digits are all different (and do not include zero)   and   n   is divisible by each of its individual digits.

135   is evenly divisible by   1,   3,   and   5.

Note that the digit zero (0) can not be in the number as integer division by zero is undefined. The digits must all be unique so a base ten number will have at most 9 digits. Feel free to use analytics and clever algorithms to reduce the search space your example needs to visit, but it must do an actual search. (Don't just feed it the answer and verify it is correct.)

Do the same thing for hexadecimal.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Largest number divisible by its digits step by step in the Java programming language

The provided Java code is designed to find the highest number with unique digits (excluding 0 and 5) that is divisible by all of its digits. This concept is known as a Lynch-Bell number.

A step-by-step explanation of the code:

  1. Constants and Initialization:

    • s is a string initialized to an empty string. It will store the string representation of the number being tested.
    • main method is the entry point of the program where the execution starts.
  2. Searching for the Highest Lynch-Bell Number:

    • The code starts with a high number i (98764321) and initializes two Boolean variables: isUnique and canBeDivided.
    • It enters a while loop that continues as long as i is greater than 0. Inside the loop:
      • s is updated with the string representation of i.
      • isUnique is set to true initially and becomes false if any digits in i are not unique (excluding 0 and 5). This check is performed by the uniqueDigits method.
      • If isUnique is true, i.e., i has unique digits, the testNumber method is called to check if i is divisible by all its digits. If testNumber returns true, it means i is a Lynch-Bell number. In this case, it is printed, and the loop terminates.
      • If uniqueDigits returns false or testNumber returns false, the loop continues to the next i value.
  3. Unique Digits Check (uniqueDigits Method):

    • This method takes an integer i as input and checks if the individual digits of i are unique (excluding 0 and 5). It iterates through the digits of i and compares them to each other. If any digits are repeated or equal to 0 or 5, it returns false. Otherwise, it returns true.
  4. Divisibility Test (testNumber Method):

    • This method takes an integer i as input and tests if i is divisible by all its digits. It iterates through the digits of i and checks if i is divisible by each digit. If any digit does not divide i (excluding 0), it returns false. Otherwise, it returns true.

Source code in the java programming language

public class LynchBell {
    
    static String s = "";
    
    public static void main(String args[]) {
        //Highest number with unique digits (no 0 or 5)
        int i = 98764321;
        boolean isUnique = true;
        boolean canBeDivided = true;
        while (i>0) {
            s = String.valueOf(i);
            isUnique = uniqueDigits(i);
            if (isUnique) {
                //Number has unique digits
                canBeDivided = testNumber(i);
                if(canBeDivided) {
                    System.out.println("Number found: " + i);
                    i=0;
                }
            }
            i--;
        }
    }
    
    public static boolean uniqueDigits(int i) {
        //returns true, if unique digits, false otherwise
        for (int k = 0; k<s.length();k++) {
            for(int l=k+1; l<s.length();l++) {
                if(s.charAt(l)=='0' || s.charAt(l)=='5') {
                    //0 or 5 is a digit
                    return false;
                }
                if(s.charAt(k) == s.charAt(l)) {
                    //non-unique digit
                    return false;
                }
            }
        }
        return true;
    }
    
    public static boolean testNumber(int i) {
        //Tests, if i is divisible by all its digits (0 is not a digit already)
        int j = 0;
        boolean divisible = true;
        // TODO: divisible by all its digits 
        for (char ch: s.toCharArray()) {
            j = Character.getNumericValue(ch);
            divisible = ((i%j)==0);
            if (!divisible) {
                return false;
            }
        }       
        return true;
    }
}


  

You may also check:How to resolve the algorithm Loops/For step by step in the blz programming language
You may also check:How to resolve the algorithm Terminal control/Cursor movement step by step in the Racket programming language
You may also check:How to resolve the algorithm List comprehensions step by step in the OCaml programming language
You may also check:How to resolve the algorithm Gray code step by step in the Euphoria programming language
You may also check:How to resolve the algorithm Pick random element step by step in the PL/I programming language