Created
December 14, 2013 12:32
-
-
Save kuoe0/7958666 to your computer and use it in GitHub Desktop.
check integer is divisible by 3 using division operation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# vim:fenc=utf-8 | |
# | |
# Copyright © 2013 KuoE0 <[email protected]> | |
# | |
# Distributed under terms of the MIT license. | |
""" | |
""" | |
def add_digits(x): | |
if x >= 10: | |
return add_digits_recurion(x // 10) + (x % 10) | |
return x | |
def add_digits_iteration(x): | |
ret = 0 | |
while x >= 10: | |
ret = ret + (x // 10) | |
x = x % 10 | |
return ret | |
def divisible_3(x): | |
while x >= 10: | |
x = add_digits_iteration(x) | |
return x in [0, 3, 6, 9] | |
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