Last active
October 3, 2016 14:28
-
-
Save husobee/9bbf57af3c531b7cefd8a5b8fe7a18f7 to your computer and use it in GitHub Desktop.
submission for codewars.com digital root
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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