Skip to content

Instantly share code, notes, and snippets.

@husobee
Last active October 3, 2016 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save husobee/9bbf57af3c531b7cefd8a5b8fe7a18f7 to your computer and use it in GitHub Desktop.
Save husobee/9bbf57af3c531b7cefd8a5b8fe7a18f7 to your computer and use it in GitHub Desktop.
submission for codewars.com digital root
def digital_root(n):
""" digital_root - recursively figure out the digital root of a number"""
s = 0 # s -> running sum
while n > 0:
# take each digit from the number by running mod 10 (ones position in base 10 number)
s += int(n%10)
# reduce the number by a factor of 10 to remove the ones position
n = int(n/10)
if s >= 10:
# if the num is greater than or equal to 10 recursively call self
return digital_root(s)
# return the sum
return s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment