check integer divisible by 3 using alternative sum
#! /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 alt_add_digits_recursion(x, sign):
if x > 1:
return alt_add_digits(x >> 1, sign * -1) + (x & 1) * sign
return (x & 1) * sign
def alt_add_digits_iteration(x):
ret = 0
sign = 1
while x > 1:
ret = ret + (x & 1) * sign
x = x >> 1
sign = sign * -1
return ret
def alt_add_digits_iteration_no_mul(x):
ret = 0
sign = True
while x > 1:
ret = ret + (x & 1) if sign else ret - (x & 1)
x = x >> 1
sign = not sign
return ret
def divisible_3(x):
while x > 1:
x = alt_add_digits_iteration_no_mul(x)
x = x if x >= 0 else -x
return x == 0
if __name__ == "__main__":
import sys
for i in range(int(sys.argv[1])):
print i, divisible_3(i)