Given an integer num, repeatedly add all its digits until the result has only one digit, and return it.

Example 1:

Input: num = 38 Output: 2 Explanation: The process is 38 --> 3 + 8 --> 11 11 --> 1 + 1 --> 2 Since 2 has only one digit, return it.

Example 2:

Input: num = 0 Output: 0

Constraints:

  • 0 <= num <= 231 - 1

Follow up: Could you do it without any loop/recursion in O(1) runtime?


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

    integer :: num
    integer :: i
    integer :: sum

    num = 38
    write (*, '(A)') 'Example 1:'
    write (*, '(A, I0)') 'Input: ', num
    write (*, '(A, I0)') 'Output:', solve(num)

    num = 0
    write (*, '(A)') 'Example 2:'
    write (*, '(A, I0)') 'Input: ', num
    write (*, '(A, I0)') 'Output:', solve(num)

contains

    function solve(num) result(sum)
        implicit none
        integer, value :: num
        integer :: sum
        integer :: digit

        sum = num
        do while (sum >= 10)
            digit = mod(sum, 10)
            sum = sum / 10 + digit
        end do
    end function solve

end program main
Compiled
Executed
Correct
! This program solves the problem of repeatedly adding all the digits of a number until the result has only one digit.

! Example 1:
! Input: num = 38
! Output: 2
! Explanation: The process is
! 38 --> 3 + 8 --> 11
! 11 --> 1 + 1 --> 2
! Since 2 has only one digit, return it.

! Example 2:
! Input: num = 0
! Output: 0

! Constraints:
! 0 <= num <= 231 - 1

! Follow up: Could you do it without any loop/recursion in O(1) runtime?

program sum_digits

implicit none

integer :: num, sum, digit, result

! Read the input number
read(*,*) num

! Initialize the sum and result
sum = 0
result = 0

! Loop until the result has only one digit
do while (result < 10)

    ! Add all the digits of the current number
    do digit = 1, num
        sum = sum + mod(num, 10)
    end do

    ! Divide the sum by 10 to get the result
    result = sum / 10

    ! Reset the sum
    sum = 0

end do

! Output the result
write(*,*) result

end program sum_digits
🌐 Data from online sources
def addDigits(num: int) -> int:
    return 1 + (num - 1) % 9

The idea is to find the pattern for the single digit that will be the outcome of repeatedly summing up a number's digits. Observe that for numbers 1-9, the single digit outcome is the number itself. For numbers 10-18 (i.e., 1+0, 1+1, 1+2, ..., 1+8), the single digit outcome is 1-9 again. This pattern repeats.

In the given code, we return the value computed from the formula 1 + (num - 1) % 9. The formula derives from the observation mentioned above. Here's the explanation:

  • Subtracting 1 shifts the sequence to range from 0 to 8 instead of 1 to 9.
  • Taking num modulo 9 reveals the repeating 0-8 pattern.
  • Adding 1 offsets the range back to 1-9.

This algorithm works for all positive integer numbers, and the result is the single digit number that is the outcome of repeatedly summing up the original number's digits.

🌐 Data from online sources
int addDigits(int num) {
    return 1 + (num - 1) % 9;
}

The idea is to find the pattern for the single digit that will be the outcome of repeatedly summing up a number's digits. Observe that for numbers 1-9, the single digit outcome is the number itself. For numbers 10-18 (i.e., 1+0, 1+1, 1+2, ..., 1+8), the single digit outcome is 1-9 again. This pattern repeats.

In the given code, we return the value computed from the formula 1 + (num - 1) % 9. The formula derives from the observation mentioned above. Here's the explanation:

  • Subtracting 1 shifts the sequence to range from 0 to 8 instead of 1 to 9.
  • Taking num modulo 9 reveals the repeating 0-8 pattern.
  • Adding 1 offsets the range back to 1-9.

This algorithm works for all positive integer numbers, and the result is the single digit number that is the outcome of repeatedly summing up the original number's digits.