Problem 180877 · medium · Level 01 Prerequisites & Setup

Folding a Number Down to One Digit

loops · while · digits · nested loops

Old bookkeepers checked their sums by casting out nines: they added up the digits of a number, and while that total still had two or more digits they added up the digits of the total, until only one digit was left. Write digital_root(n) that takes a non-negative integer n and returns the digit the bookkeeper ends with.

Examples

Input:  n = 493
Output: 7
Explanation: 4 + 9 + 3 = 16, then 1 + 6 = 7.

Input:  n = 9
Output: 9

Constraints

  • 0 <= n <= 10**18

Goals

  • Nest a digit-summing loop inside an outer repeat-until loop
  • Recognise when a repeated process has reached its stopping point
Starting Python…