mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-14 08:01:05 +01:00
24 lines
494 B
Ruby
24 lines
494 B
Ruby
|
# Challenge name: Add Digits
|
||
|
#
|
||
|
# Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
|
||
|
#
|
||
|
# Example:
|
||
|
#
|
||
|
# Input: 38
|
||
|
# Output: 2
|
||
|
# Explanation: The process is like: 3 + 8 = 11, 1 + 1 = 2.
|
||
|
# Since 2 has only one digit, return it.
|
||
|
#
|
||
|
# Follow up:
|
||
|
# Could you do it without any loop/recursion in O(1) runtime?
|
||
|
# @param {Integer} num
|
||
|
# @return {Integer}
|
||
|
|
||
|
#
|
||
|
# Approach 1: Brute Force
|
||
|
#
|
||
|
def add_digits(num)
|
||
|
end
|
||
|
|
||
|
puts(add_digits(38))
|
||
|
# Output: 2
|