Hacker News new | ask | show | jobs
by schoen 3971 days ago
Try this fun Python program to see how multiples of 9 are always generated no matter how you combine the digits:

  #!/usr/bin/env python
  
  import random
  
  digits = [1, 4, 2, 8, 5, 7]
  
  for times in range(20):
      digits_left = digits[:]
      nums = []
      while digits_left:
         this_num = 0
         for i in range(random.randint(1, len(digits_left))):
             digit = random.choice(digits_left)
             this_num *= 10
             this_num += digit
             digits_left.remove(digit)
         nums.append(this_num)
      print " + ".join(map(str, nums)), "=", sum(nums), "(a multiple of 9)"
Sample output:

  4271 + 58 = 4329 (a multiple of 9)
  7412 + 58 = 7470 (a multiple of 9)
  8 + 124 + 5 + 7 = 144 (a multiple of 9)
  845127 = 845127 (a multiple of 9)
  147285 = 147285 (a multiple of 9)
  1 + 824 + 75 = 900 (a multiple of 9)
  5 + 2 + 481 + 7 = 495 (a multiple of 9)
  758 + 41 + 2 = 801 (a multiple of 9)
  47125 + 8 = 47133 (a multiple of 9)
  7584 + 12 = 7596 (a multiple of 9)
  1275 + 8 + 4 = 1287 (a multiple of 9)
  475 + 12 + 8 = 495 (a multiple of 9)
  12 + 5 + 478 = 495 (a multiple of 9)
  4251 + 7 + 8 = 4266 (a multiple of 9)
  185 + 274 = 459 (a multiple of 9)
  28514 + 7 = 28521 (a multiple of 9)
  41278 + 5 = 41283 (a multiple of 9)
  457182 = 457182 (a multiple of 9)
  845712 = 845712 (a multiple of 9)
  2718 + 54 = 2772 (a multiple of 9)