Skip to content

Instantly share code, notes, and snippets.

@jeffp123
Created November 6, 2015 01:48
Show Gist options
  • Save jeffp123/0ec74fcac48a8ac1c851 to your computer and use it in GitHub Desktop.
Save jeffp123/0ec74fcac48a8ac1c851 to your computer and use it in GitHub Desktop.
A script to demonstrate the idea that starting at a multiple of 9 and dividing by 2 will yield digits that add up to 9
"""A script to demonstrate the idea that starting at a multiple of 9 and dividing by 2
will yield digits that add up to 9"""
import sys
from decimal import *
DEFAULT_STARTING_VALUE = 360
DECIMAL_PRECISION = 255
def _get_num_str(num):
return ("%.255e" % num).split('e')[0].replace('.', '').strip('0')
def _sum_digits(num_str):
return sum(int(i) for i in num_str)
def _is_divisible_by_nine(num):
return (num % 9) == 0
def main(args):
num = args[0] if len(args) else DEFAULT_STARTING_VALUE
getcontext().prec = DECIMAL_PRECISION
num = Decimal(num)
while True:
num_str = _get_num_str(num)
if len(num_str) > getcontext().prec:
break
digits_sum = _sum_digits(num_str)
print num_str, digits_sum, _is_divisible_by_nine(digits_sum)
num = Decimal(num / 2)
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
@jeffp123
Copy link
Author

jeffp123 commented Nov 6, 2015

Sample output:

36 9 True
18 9 True
9 9 True
45 9 True
225 9 True
1125 9 True
5625 18 True
28125 18 True
140625 18 True
703125 18 True
3515625 27 True
17578125 36 True
87890625 45 True
439453125 36 True
2197265625 45 True
10986328125 45 True
54931640625 45 True
274658203125 45 True
1373291015625 45 True
6866455078125 63 True
34332275390625 54 True
171661376953125 63 True
858306884765625 81 True
4291534423828125 63 True
21457672119140625 63 True
107288360595703125 72 True
536441802978515625 81 True
2682209014892578125 81 True
13411045074462890625 72 True
67055225372314453125 72 True
335276126861572265625 90 True
1676380634307861328125 90 True
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment