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?
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
Example 1: Input: 38 Output:2 Example 2: Input: 0 Output:0
! 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
At line 27 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7aaae6e67960 in ??? #1 0x7aaae6e684d9 in ??? #2 0x7aaae70bc17b in ??? #3 0x7aaae70b5684 in ??? #4 0x7aaae70b62aa in ??? #5 0x580b62ef2209 in MAIN__ #6 0x580b62ef2344 in main
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:
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.
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:
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.