Digital root

Given a natural number, n, its digital root is defined as the result of adding its digits, and iterating this process with the new number until you reach a single digit number. This digit is called the digital root of n. For example,

\(374 \Rightarrow 3 + 4 + 7 = 14 \Rightarrow 1 + 4 = 5 \Rightarrow digital\_root(374) = 5\)

Save all these functions into a file named: digitalroot.py

  1. Write function add_digits() that given an integer, n, returns the addition of its digits. Examples:

    >>> add_digits(374)
    14
    >>> add_digits(14)
    5
    >>> add_digits(10001)
    2
    

    Note

    More tests are provided in file add_digits.txt

  2. Write function digital_root() that from an integer, n, returns its digital root. You must use the previous function add_digits(). Examples:

    >>> digital_root(7)
    7
    >>> digital_root(26)
    8
    >>> digital_root(374)
    5
    >>> digital_root(69870)
    3
    >>> digital_root(9898999887998978799)
    6
    

    Note

    More tests are provided in file digital_root.txt

  3. Write function is_digital_root() that given two integers, n and r, returns True if r is the digital root of n or False otherwise. You must use the previous function. Examples:

    >>> is_digital_root(7, 7)
    True
    >>> is_digital_root(26, 8)
    True
    >>> is_digital_root(26, 6)
    False
    >>> is_digital_root(374, 5)
    True
    

    Note

    More tests are provided in file is_digital_root.txt

  4. Write function is_partial_add() that given two integers, n and p, returns True if p is a partial addition obtained during the calculation of the digital root of n or False, otherwise. For example: 5 and 14 are partial additions of 374. To write this function you can get inspiration from function digital_root(). Examples:

    >>> is_partial_add(26, 8)
    True
    >>> is_partial_add(9, 9)
    False
    >>> is_partial_add(2634, 15)
    True
    >>> is_partial_add(2634, 6)
    True
    

    Note

    More tests are provided in file is_partial_add.txt

Solution

A solution of these functions is provided in file digitalroot.py.