forked from CoderAcademy-BRI/ruby-challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path33_digital_root.rb
More file actions
24 lines (19 loc) · 791 Bytes
/
Copy path33_digital_root.rb
File metadata and controls
24 lines (19 loc) · 791 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Digital root
# The digital root (also repeated digital sum) of a
# non-negative integer is the (single digit) value
# obtained by an iterative process of summing digits,
# on each iteration using the result from the previous
# iteration to compute a digit sum.
# The process continues until a single-digit number is reached.
# For example, the digital root of 65,536 is 7,
# because 6 + 5 + 5 + 3 + 6 = 25 and 2 + 5 = 7.
# Your code should return the digital root of any positive
# number passed in.
# You can assume the argument given is an integer.
# Return -1 if any negative argument is provided.
# Stupid recursive solution: do not use
def digital_root(number)
return -1 if number < 0
return number if number % 10 == number
return digital_root(number.digits.sum)
end