Skip to content

Instantly share code, notes, and snippets.

@kuoe0
Created December 14, 2013 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuoe0/7958684 to your computer and use it in GitHub Desktop.
Save kuoe0/7958684 to your computer and use it in GitHub Desktop.
check integer divisible by 3 using hex
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2013 KuoE0 <kuoe0.tw@gmail.com>
#
# Distributed under terms of the MIT license.
"""
"""
def add_digits_16_recursion(x):
if x >= 0x10:
return add_digits_16(x >> 4) + (x & 0xF)
return x
def add_digits_16_iteration(x):
ret = 0
while x >= 0x10:
ret = ret + (x & 0xF)
x = x >> 4
return ret
def divisible_3(x):
while x >= 0x10:
x = add_digits_16_iteration(x)
return x in [0x0, 0x3, 0x6, 0x9, 0xC, 0xF]
if __name__ == "__main__":
import sys
for i in range(int(sys.argv[1])):
print i, divisible_3(i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment