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,
.. raw:: html
:math:`374 \Rightarrow
3 + 4 + 7 = 14 \Rightarrow
1 + 4 = 5 \Rightarrow
digital\_root(374) = 5`
.. raw:: html
Save all these functions into a file named: ``digitalroot.py``
#. Write function :py:func:`add_digits` that given an integer, ``n``,
returns the addition of its digits. Examples:
.. literalinclude:: add_digits.txt
:language: python3
:lines: 3-
.. note::
More tests are provided in file :download:`add_digits.txt `
#. Write function :py:func:`digital_root` that from an integer,
``n``, returns its digital root. You must use the previous function
:py:func:`add_digits`. Examples:
.. literalinclude:: digital_root.txt
:language: python3
:lines: 3-
.. note::
More tests are provided in file :download:`digital_root.txt `
#. Write function :py:func:`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:
.. literalinclude:: is_digital_root.txt
:language: python3
:lines: 3-10
.. note::
More tests are provided in file :download:`is_digital_root.txt `
#. Write function :py:func:`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 :py:func:`digital_root`. Examples:
.. literalinclude:: is_partial_add.txt
:language: python3
:lines: 3-10
.. note::
More tests are provided in file :download:`is_partial_add.txt `
.. rubric:: Solution
A solution of these functions is provided in file :download:`digitalroot.py
`.