A self-dividing number is a number that is divisible by every digit it contains.
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
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
temp.f95:23:22: 11 | do i = left, right | 2 ...... 23 | i = i / 10 | 1 Error: Variable ‘i’ at (1) cannot be redefined inside loop beginning at (2)
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
temp.f95:7:30: 7 | function self_dividing_numbers(left, right) result(output) | 1 Error: MODULE attribute of ‘self_dividing_numbers’ conflicts with PROCEDURE attribute at (1) temp.f95:9:34: 9 | integer, intent(in) :: left, right | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:26: 10 | integer :: i, j, output(0) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:12:18: 12 | do i = left, right | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:16:5: 16 | j = i | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:18:16: 18 | do while (j > 0) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:20:24: 20 | if (mod(i, j) /= 0) exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:21:10: 21 | j = j / 10 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:23:3: 23 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:25:16: 25 | if (j == 0) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:29:20: 29 | output = [output, i] | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:31:3: 31 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:33:3: 33 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:35:3: 35 | end function self_dividing_numbers | 1 Error: Expecting END MODULE statement at (1) temp.f95:41:5: 41 | use self_dividing_numbers | 1 Fatal Error: Cannot open module file ‘self_dividing_numbers.mod’ for reading at (1): No such file or directory compilation terminated.
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.
#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.