Created
April 10, 2018 20:17
-
-
Save onjin/047a5c7d805a1ffa4d052632c3d9024f to your computer and use it in GitHub Desktop.
Calculate digital root with modulo arithmetic.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
def root(n): | |
"""Calculate digital root with modulo arithmetic.""" | |
return n % 9 or n and 9 | |
if __name__ == "__main__": | |
for i in range(9): | |
assert i == root(i) | |
for i in range(9, 200*9, 9): | |
assert 9 == root(i) | |
for i in range(10, 17): | |
assert i-9 == root(i) | |
assert root(123) == 6 | |
assert root(321) == 6 | |
assert root(997) == 7 | |
assert root(7593329) == 2 | |
assert root(root(321) + root(123)) == root(444) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment