Digital root
หน้านี้ต้องใช้เบราว์เซอร์ล่าสุด (รองรับ SharedArrayBuffer) กรุณาอัปเดต Chrome, Edge, Firefox หรือ Safari เป็นเวอร์ชันล่าสุด
อันนี้สร้างจากบทเรียนที่คุณยังไม่เสร็จสิ้น ให้ทำบทเรียนนั้นก่อน:
Python Basics · Functions →
Digital root
คงเพิ่ม digit ต่อไปจนกว่าจะมี digit เดียวเหลืออยู่
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.
ความยาก 4/5 · 280 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
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.
คลิก Run เพื่อดูผลลัพธ์ที่นี่