Self Dividing Numbers

🏠 ⬅️ ➡️

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.

A self-dividing number is not allowed to contain the digit zero.

Given two integers left and right, return a list of all the self-dividing numbers in the range [left, right].

Example 1:

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

Example 2:

Input: left = 47, right = 85 Output: [48,55,66,77]

Constraints:

  • 1 <= left <= right <= 104

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer :: i, j, n, left, right
    logical :: is_self_dividing

    ! read input
    read(*,*) left, right

    ! solve problem
    do i = left, right
        is_self_dividing = .true.
        do while (i > 0)
            n = mod(i, 10)
            if (n == 0) then
                is_self_dividing = .false.
                exit
            end if
            if (mod(i, n) /= 0) then
                is_self_dividing = .false.
                exit
            end if
            i = i / 10
        end do
        if (is_self_dividing) then
            write (*,'(I0)') i
        end if
    end do

end program main
Compiled
Executed
Correct
module self_dividing_numbers

implicit none

contains

function self_dividing_numbers(left, right) result(output)

integer, intent(in) :: left, right
integer :: i, j, output(0)

do i = left, right

! Check if the number is self-dividing

j = i

do while (j > 0)

if (mod(i, j) /= 0) exit
j = j / 10

end do

if (j == 0) then

! Add the number to the output list

output = [output, i]

end if

end do

end function self_dividing_numbers

end module

program test

use self_dividing_numbers

implicit none

integer :: left, right, i, output(0)

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

left = 1
right = 22
output = self_dividing_numbers(left, right)

do i = 1, size(output)

write (*,*) output(i)

end do

! Test case 2: [48,55,66,77]

left = 47
right = 85
output = self_dividing_numbers(left, right)

do i = 1, size(output)

write (*,*) output(i)

end do

end program
🌐 Data from online sources
def is_self_dividing(num):
    n = num
    while n:
        digit = n % 10
        if digit == 0 or num % digit != 0:
            return False
        n //= 10
    return True

def self_dividing_numbers(left, right):
    return [i for i in range(left, right+1) if is_self_dividing(i)]

The algorithm consists of two functions: isSelfDividing and selfDividingNumbers. The first function, isSelfDividing, takes an integer as input and returns true if it is a self-dividing number and false otherwise. It does this by checking each digit of the number and verifying that they're nonzero and that the number is divisible by that digit. The checking is done by repeatedly dividing the number by 10 and finding the remainder.

The second function, selfDividingNumbers, takes left and right as input, and returns a list of self-dividing numbers in the given range. It iterates through all the numbers between left and right (inclusive) and checks if the number is a self-dividing number using the isSelfDividing function. If it is, the number is added to the result list. Finally, the result list is returned.

🌐 Data from online sources
#include <vector>

bool isSelfDividing(int num) {
    int n = num;
    while (n) {
        int digit = n % 10;
        if (digit == 0 || num % digit != 0)
            return false;
        n /= 10;
    }
    return true;
}

std::vector<int> selfDividingNumbers(int left, int right) {
    std::vector<int> result;
    for (int i = left; i <= right; ++i) {
        if (isSelfDividing(i))
            result.push_back(i);
    }
    return result;
}

The algorithm consists of two functions: isSelfDividing and selfDividingNumbers. The first function, isSelfDividing, takes an integer as input and returns true if it is a self-dividing number and false otherwise. It does this by checking each digit of the number and verifying that they're nonzero and that the number is divisible by that digit. The checking is done by repeatedly dividing the number by 10 and finding the remainder.

The second function, selfDividingNumbers, takes left and right as input, and returns a list of self-dividing numbers in the given range. It iterates through all the numbers between left and right (inclusive) and checks if the number is a self-dividing number using the isSelfDividing function. If it is, the number is added to the result list. Finally, the result list is returned.