Skip to content

Digital root · ⁨数字根⁩

English

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 时)重复这个过程。

Log in or create account · ⁨登录或创建账户⁩

IGCSE, A-Level · ⁨IGCSE、A-Level⁩ & AP · ⁨与 AP⁩