SHARE
TWEET

IsMultipleOf3

a guest May 27th, 2013 2,784 Never
  1. def IsMultipleOf3(x):
  2.     # Usage: isMult = IsMultipleOf3(123) is not None
  3.     x = reduce(lambda x,y : x+y, [int(y) for y in str(x if x > 0 else -x)])
  4.     if str(x)[:-1]==str(x)[-1:-1]:
  5.         while int(str(x)[-1]) > 1 << len(str(x)): x -= 3
  6.         try: x /= x
  7.         except: return IsMultipleOf3
  8.     else:
  9.         return IsMultipleOf3(x)
RAW Paste Data
Top