##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/728.html

Easy

## Description

A self-dividing number is a number that is divisible by every digit it contains.

For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0.

Also, a self-dividing number is not allowed to contain the digit zero.

Given a lower and upper number bound, output a list of every possible self dividing number, including the bounds if possible.

Example 1:

Input:

left = 1, right = 22

Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]

Note:

• The boundaries of each input argument are 1 <= left <= right <= 10000.

## Solution

For each number in the range [left, right], check whether the number is a self-dividing number.

To check whether a number is a self-dividing number, loop over all its digits and check each digit. If there exists a digit that is 0 or a digit such that the number is not divisible by the digit, then the number is not a self-dividing number. Otherwise, the number is a self-dividing number.

• import java.util.ArrayList;
import java.util.List;

/**

A self-dividing number is a number that is divisible by every digit it contains.

For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0.

Also, a self-dividing number is not allowed to contain the digit zero.

Given a lower and upper number bound, output a list of every possible self dividing number, including the bounds if possible.

Example 1:
Input:
left = 1, right = 22
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]
Note:

The boundaries of each input argument are 1 <= left <= right <= 10000.

*/

public class Self_Dividing_Numbers {

public static void main (String[] args) {
Self_Dividing_Numbers out = new Self_Dividing_Numbers();
Solution s = out.new Solution();

System.out.println(s.selfDividingNumbers(21, 22));
}

class Solution {

List<Integer> list = new ArrayList<>();

public List<Integer> selfDividingNumbers(int left, int right) {
if (left > right || right <= 0) {
return null;
}

for (int i = left; i <= right; i++) {
isSeflDividing(i);
}

return list;
}

public void isSeflDividing(int n) {

List<Integer> digits = new ArrayList<>();
int current = n;
while (current > 0) {
int d = current % 10;
if (d == 0) {
return;
}
current /= 10;
}

for (int each: digits) {
if (n % each != 0) {
return;
}
}

}
}
}

############

class Solution {
public List<Integer> selfDividingNumbers(int left, int right) {
List<Integer> ans = new ArrayList<>();
for (int i = left; i <= right; ++i) {
if (check(i)) {
}
}
return ans;
}

private boolean check(int num) {
for (int t = num; t != 0; t /= 10) {
int x = t % 10;
if (x == 0 || num % x != 0) {
return false;
}
}
return true;
}
}

• // OJ: https://leetcode.com/problems/self-dividing-numbers/
// Time: O(ND) where N = right - left + 1, D = digit count of right.
// Space: O(1)
class Solution {
private:
bool isSelfDividing(int n) {
int m = n;
while (m) {
int d = m % 10;
if (!d || n % d != 0) return false;
m /= 10;
}
return true;
}
public:
vector<int> selfDividingNumbers(int left, int right) {
vector<int> ans;
for (int i = left; i <= right; ++i) {
if (isSelfDividing(i)) ans.push_back(i);
}
return ans;
}
};

• class Solution:
def selfDividingNumbers(self, left: int, right: int) -> List[int]:
return [
num
for num in range(left, right + 1)
if all(i != '0' and num % int(i) == 0 for i in str(num))
]

############

class Solution:
def isDividingNumber(self, num):
if '0' in str(num):
return False
return 0 == sum(num % int(i) for i in str(num))
def selfDividingNumbers(self, left, right):
"""
:type left: int
:type right: int
:rtype: List[int]
"""
for num in range(left, right+1):
print(num)
if self.isDividingNumber(num):

• func selfDividingNumbers(left int, right int) []int {
check := func(num int) bool {
for t := num; t != 0; t /= 10 {
x := t % 10
if x == 0 || num%x != 0 {
return false
}
}
return true
}

var ans []int
for i := left; i <= right; i++ {
if check(i) {
ans = append(ans, i)
}
}
return ans
}

• impl Solution {
pub fn self_dividing_numbers(left: i32, right: i32) -> Vec<i32> {
let mut res = vec![];
for i in left..=right {
let mut num = i;
if loop {
if num == 0 {
break true;
}
let j = num % 10;
if j == 0 || i % j != 0 {
break false;
}
num /= 10;
} {
res.push(i);
}
}
res
}
}


• class Solution {
public List<Integer> selfDividingNumbers(int left, int right) {
List<Integer> list = new ArrayList<Integer>();
for (int i = left; i <= right; i++) {
if (isSelfDividing(i))
}
return list;
}

public boolean isSelfDividing(int num) {
char[] digitsArray = String.valueOf(num).toCharArray();
for (char c : digitsArray) {
int digit = c - '0';
if (digit == 0 || num % digit != 0)
return false;
}
return true;
}
}

############

class Solution {
public List<Integer> selfDividingNumbers(int left, int right) {
List<Integer> ans = new ArrayList<>();
for (int i = left; i <= right; ++i) {
if (check(i)) {
}
}
return ans;
}

private boolean check(int num) {
for (int t = num; t != 0; t /= 10) {
int x = t % 10;
if (x == 0 || num % x != 0) {
return false;
}
}
return true;
}
}

• // OJ: https://leetcode.com/problems/self-dividing-numbers/
// Time: O(ND) where N = right - left + 1, D = digit count of right.
// Space: O(1)
class Solution {
private:
bool isSelfDividing(int n) {
int m = n;
while (m) {
int d = m % 10;
if (!d || n % d != 0) return false;
m /= 10;
}
return true;
}
public:
vector<int> selfDividingNumbers(int left, int right) {
vector<int> ans;
for (int i = left; i <= right; ++i) {
if (isSelfDividing(i)) ans.push_back(i);
}
return ans;
}
};

• class Solution:
def selfDividingNumbers(self, left: int, right: int) -> List[int]:
return [
num
for num in range(left, right + 1)
if all(i != '0' and num % int(i) == 0 for i in str(num))
]

############

class Solution:
def isDividingNumber(self, num):
if '0' in str(num):
return False
return 0 == sum(num % int(i) for i in str(num))
def selfDividingNumbers(self, left, right):
"""
:type left: int
:type right: int
:rtype: List[int]
"""
for num in range(left, right+1):
print(num)
if self.isDividingNumber(num):

• func selfDividingNumbers(left int, right int) []int {
check := func(num int) bool {
for t := num; t != 0; t /= 10 {
x := t % 10
if x == 0 || num%x != 0 {
return false
}
}
return true
}

var ans []int
for i := left; i <= right; i++ {
if check(i) {
ans = append(ans, i)
}
}
return ans
}

• impl Solution {
pub fn self_dividing_numbers(left: i32, right: i32) -> Vec<i32> {
let mut res = vec![];
for i in left..=right {
let mut num = i;
if loop {
if num == 0 {
break true;
}
let j = num % 10;
if j == 0 || i % j != 0 {
break false;
}
num /= 10;
} {
res.push(i);
}
}
res
}
}