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. · לחץ על הרץ כדי לראות את התוצא כאן.