-
Notifications
You must be signed in to change notification settings - Fork 0
/
digi_root.py
51 lines (36 loc) · 1.08 KB
/
digi_root.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
__author__ = 'saurav'
"""
Program to find the digital root of a number
http://en.wikipedia.org/wiki/Digital_root
Digital Root can be used as a poor man's crude hashing techniques
Usage: python digi_root.py <Input Number>
"""
import sys
input_num = 0
def got_two_more(digi_root):
global input_num
quotient = digi_root
digi_root = (quotient / 10) + (quotient % 10)
# print digi_root, quotient
if len(str(digi_root)) == 2:
got_two_more(digi_root)
else:
print 'The digital root of ' + str(input_num) + ' is: ' + str(digi_root)
def main():
global input_num
input_num = sys.argv[1]
length = len(str(input_num))
digi_root = 0
quotient = int(input_num)
for i in xrange(length):
digi_root += quotient % 10
tmp_quotient = quotient / 10
quotient = tmp_quotient
#print digi_root, quotient
if len(str(digi_root)) == 2:
got_two_more(digi_root)
else:
print 'The digital root of ' + str(input_num) + ' is: ' + str(digi_root)
if __name__ == "__main__":
main()