Count the Digits That Divide a Number

🏠 ⬅️ ➑️

Given an integer num, return the number of digits in num that divide num.

An integer val divides nums if nums % val == 0.

Example 1:

Input: num = 7 Output: 1 Explanation: 7 divides itself, hence the answer is 1.

Example 2:

Input: num = 121 Output: 2 Explanation: 121 is divisible by 1, but not 2. Since 1 occurs twice as a digit, we return 2.

Example 3:

Input: num = 1248 Output: 4 Explanation: 1248 is divisible by all of its digits, hence the answer is 4.

Constraints:

  • 1 <= num <= 109
  • num does not contain 0 as one of its digits.

Note: This problem is from LeetCode.
βœ… Compiled
βœ… Executed
❌ Correct
program main
    ! Solves the problem: Given an integer `num`, return the number of digits in `num` that divide `num`.
    implicit none

    integer :: num, i, j, count
    character(len=10) :: str

    ! Examples
    num = 7
    call solve(num)
    write (*,*) "Example 1:"
    write (*,*) "Input:", num
    write (*,*) "Output:", count
    write (*,*) ""

    num = 121
    call solve(num)
    write (*,*) "Example 2:"
    write (*,*) "Input:", num
    write (*,*) "Output:", count
    write (*,*) ""

    num = 1248
    call solve(num)
    write (*,*) "Example 3:"
    write (*,*) "Input:", num
    write (*,*) "Output:", count
    write (*,*) ""

contains

    subroutine solve(num)
        implicit none
        integer, intent(in) :: num
        integer :: i, j
        character(len=10) :: str

        ! Convert `num` to string
        write (str, '(I10)') num

        ! Count the number of digits that divide `num`
        count = 0
        do i = len(str), 1, -1
            do j = 1, i-1
                if (str(j:i) == str(i:)) then
                    count = count + 1
                    exit
                end if
            end do
        end do
    end subroutine solve
end program main
❌ Compiled
❌ Executed
❌ Correct
! Declare the input variable
integer :: num

! Declare the output variable
integer :: result

! Read the input
read(*,*) num

! Initialize the result
result = 0

! Loop through the digits of num
do i = 1, len_trim(num)
    ! Extract the current digit
    digit = num(i:i)

    ! Check if the digit divides num
    if (num mod digit == 0) then
        ! Increment the result
        result = result + 1
    end if
end do

! Print the result
write(*,*) result

end
🌐 Data from online sources
def digits_dividing_num(num):
    count = 0
    temp = num
    while temp > 0:
        digit = temp % 10
        if num % digit == 0:
            count += 1
        temp //= 10
    return count
The algorithm for counting the number of digits in `num` that divide `num` is as follows:
  1. Create a variable count to store the count of such digits, and set it to 0.
  2. Create a temporary variable temp equal to the given num.
  3. While temp is greater than 0, follow these steps: a. Find the last digit of temp by calculating the remainder of the division temp % 10. b. Check if num % digit is equal to 0; if so, increment the count by 1. c. Remove the last digit of temp by integer dividing it by 10 (i.e., temp /= 10 in C++ and Java, temp //= 10 in Python, or temp = Math.floor(temp / 10) in JavaScript).
  4. Return the count.

Each language implements these steps in a fairly similar way, with only minor syntax differences between them.

🌐 Data from online sources
int digitsDividingNum(int num) {
    int count = 0, temp = num;
    while (temp > 0) {
        int digit = temp % 10;
        if (num % digit == 0) {
            count++;
        }
        temp /= 10;
    }
    return count;
}
The algorithm for counting the number of digits in `num` that divide `num` is as follows:
  1. Create a variable count to store the count of such digits, and set it to 0.
  2. Create a temporary variable temp equal to the given num.
  3. While temp is greater than 0, follow these steps: a. Find the last digit of temp by calculating the remainder of the division temp % 10. b. Check if num % digit is equal to 0; if so, increment the count by 1. c. Remove the last digit of temp by integer dividing it by 10 (i.e., temp /= 10 in C++ and Java, temp //= 10 in Python, or temp = Math.floor(temp / 10) in JavaScript).
  4. Return the count.

Each language implements these steps in a fairly similar way, with only minor syntax differences between them.