Digital root · 数字根
Digital root
Keep adding digits until a single digit remains.
Two loops work together here. The inner for adds up the digits once; str(n) lets you loop over them as characters. The outer while repeats that as long as the number still has two or more digits, which is while n >= 10.
数字根
不断把各位数字相加,直到只剩一位数。
这里两个循环配合使用。里面的 for 把各位数字加一遍;用 str(n) 就能把数字当作字符来遍历。外面的 while 在数还有两位或更多位时(也就是 n >= 10 时)重复这个过程。
Write digital_root(n) that adds up the digits of n, then the digits of that answer, and so on until one digit is left. 9875 gives 29, then 11, then 2. · 编写 digital_root(n):把 n 的各位数字相加,再把结果的各位数字相加,如此重复,直到只剩一位数。9875 得到 29,然后 11,最后 2。
Click Run to see the output here. · 点击“运行”查看此处输出。