擅长:python、mysql、java
<p>这是我的看法。我很想使用<code>sum</code>,但这几乎像是作弊,因为你可以直接使用<code>sum([int(i) for i in str(n)])</code></p>
<pre><code>def digital_root(n):
# convert to a string
as_str = str(n)
# get the value of the current first digit
value = int(as_str[0])
if len(as_str) > 1:
# add the recursive return plus the value
# for anything other than our base case.
# pass the rest of the digits into our recursive call
return digital_root(int(as_str[1:])) + value
# our base case
return value
print(digital_root(493193))
</code></pre>