How to resolve the algorithm Largest number divisible by its digits step by step in the Java programming language
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:
-
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.
-
Searching for the Highest Lynch-Bell Number:
- The code starts with a high number
i
(98764321) and initializes two Boolean variables:isUnique
andcanBeDivided
. - It enters a
while
loop that continues as long asi
is greater than 0. Inside the loop:s
is updated with the string representation ofi
.isUnique
is set totrue
initially and becomesfalse
if any digits ini
are not unique (excluding 0 and 5). This check is performed by theuniqueDigits
method.- If
isUnique
istrue
, i.e.,i
has unique digits, thetestNumber
method is called to check ifi
is divisible by all its digits. IftestNumber
returnstrue
, it meansi
is a Lynch-Bell number. In this case, it is printed, and the loop terminates. - If
uniqueDigits
returnsfalse
ortestNumber
returnsfalse
, the loop continues to the nexti
value.
- The code starts with a high number
-
Unique Digits Check (
uniqueDigits
Method):- This method takes an integer
i
as input and checks if the individual digits ofi
are unique (excluding 0 and 5). It iterates through the digits ofi
and compares them to each other. If any digits are repeated or equal to 0 or 5, it returnsfalse
. Otherwise, it returnstrue
.
- This method takes an integer
-
Divisibility Test (
testNumber
Method):- This method takes an integer
i
as input and tests ifi
is divisible by all its digits. It iterates through the digits ofi
and checks ifi
is divisible by each digit. If any digit does not dividei
(excluding 0), it returnsfalse
. Otherwise, it returnstrue
.
- This method takes an integer
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