Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Digital root is the recursive sum of all the digits in a number.
- Given n, take the sum of the digits of n. If that value has more than one digit, continue reducing in this way
- until a single-digit number is produced. The input will be a non-negative integer.
- Examples
- 16 --> 1 + 6 = 7
- 942 --> 9 + 4 + 2 = 15 --> 1 + 5 = 6
- 132189 --> 1 + 3 + 2 + 1 + 8 + 9 = 24 --> 2 + 4 = 6
- 493193 --> 4 + 9 + 3 + 1 + 9 + 3 = 29 --> 2 + 9 = 11 --> 1 + 1 = 2
- '''
- def digitalRoot(n):
- if n <= 0 or n != int(n):
- print("Error while using the function '" + digitalRoot.__name__ + "'")
- return -1000
- if n >= 1 and n <= 9:
- return n
- else:
- N = str(n)
- SUM = 0
- for i in range(len(N)):
- SUM += int(N[i])
- return digitalRoot(SUM)
- # MAIN FUNCTION
- print(digitalRoot(16))
- print(digitalRoot(942))
- print(digitalRoot(132189))
- print(digitalRoot(493193))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement