Skip to content

Digit Degree

Let's define digit degree of some positive integer as the number of times we need to replace this number with the sum of its digits until we get to a one digit number.

Given an integer, find its digit degree.

Example

  • For n = 5, the output should be

    digit_degree(n) = 0
  • For n = 100, the output should be

    digit_degree(n) = 1

    1 + 0 + 0 = 1.

  • For n = 91, the output should be

    digit_degree(n) = 2

    9 + 1 = 10 -> 1 + 0 = 1.

Solution

py
def digit_degree(n):
    degree = 0

    while(n > 9):
        n = sum(list(map(int, (str(n).strip()))))
        degree += 1

    return degree


print(digit_degree(5))
print(digit_degree(100))
print(digit_degree(91))
js
function digitDegree(n) {
  let count = 0;
  while (n > 9) {
    n = n
      .toString()
      .split('')
      .reduce((a, b) => parseInt(a) + parseInt(b));
    count++;
  }
  return count;
}

console.log(digitDegree(5));
console.log(digitDegree(100));
console.log(digitDegree(91));

my thoughts are neither my employer's nor my wife's