
Self-dividing numbers are integers that are divisible by each of their digits without leaving any remainder. Specifically, no self-dividing number can contain the digit "0" since division by zero is undefined. The task is to generate a list of all self-dividing numbers that fall within a given range, defined by two integers 'left' and 'right' (both inclusive). For instance, the number 128 qualifies as a self-dividing number because it is evenly divisible by 1, 2, and 8. To solve this problem, you need to check each number in the specified range and determine whether each of its digits evenly divides the number.
Input:
Output:
Input:
Output:
1 <= left <= right <= 104The problem involves checking each number within the specified range to see if it is a self-dividing number. Here is a concise approach to solve the problem:
In the examples provided:
left = 1 to right = 22 yielded numbers such as 1 through 9 (all single-digit numbers are self-dividing) and other specific numbers up to 22 which fulfill the criteria. Notably, numbers like 10, 14, 17, etc., are excluded since they contain digits that don't divide evenly into the number.left = 47 to right = 85, fewer self-dividing numbers are found due to more digits and complex combinations that fail to be self-dividing, leading to a much smaller list compared to the first example.Using these steps, one can efficiently filter and compile the list of self-dividing numbers.
The Java code defines a class named Solution that identifies and returns a list of self-dividing numbers between two given integers, inclusive. A self-dividing number is defined as a number that is divisible by every digit it contains, where no digit in the number is zero.
Here's a quick overview of the key components and how they work:
getSelfDividingNumbers(int start, int end): This method generates a list of integers. It iterates from the start value to the end value, checking each number to see if it is self-dividing by calling the isSelfDividing(int num) method.
isSelfDividing(int num): This method determines if a number is self-dividing. It converts the number to a string, iterates through each character, and checks two conditions:
num must be divisible by this digit without leaving a remainder.If both conditions are met for every digit, the number is considered self-dividing, and the method returns true; otherwise, it returns false.
Using these definitions, the code adds only self-dividing numbers to the resulting list, which it returns upon the completion of the iteration through the range. This approach utilizes straightforward control flow and modulo operation to achieve the goal effectively.
0 Comments
Be the first to comment and share your perspective with the community.