#141BeginnerLoops· while Loops

Sum of Digits

Write `digit_sums(n)` that returns two numbers in a list: 1. the sum of the decimal digits of `n` (ignore a minus sign), and 2. the **digital root**: keep summing the digits of the result until a single digit remains. Solve it with arithmetic (`%` and `//`) inside a `while` loop rather than by converting the number to a string.

Input format

`n`: an integer.

Output format

A list `[digit_sum, digital_root]`.

Examples

Input: n = 9875
Output: [29,2]
9 + 8 + 7 + 5 = 29, then 2 + 9 = 11, then 1 + 1 = 2.
Input: n = -405
Output: [9,9]
The sign is ignored: 4 + 0 + 5 = 9, already one digit.

Constraints

  • -10^18 <= n <= 10^18

Study first

  • #python
  • #loops
  • #while
  • #modulo