Created
December 14, 2013 12:35
-
-
Save kuoe0/7958692 to your computer and use it in GitHub Desktop.
check integer divisible by 3 using finite state machine
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. | |
""" | |
""" | |
table = [[0, 1], [2, 0], [1, 2]] | |
def stat_transfer_recursion(stat, x): | |
if x > 0: | |
return stat_transfer(table[stat][x & 1], x >> 1) | |
return stat | |
def stat_transfer_iteration(stat, x): | |
stat = 0 | |
while x > 0: | |
stat = table[stat][x & 1] | |
x = x >> 1 | |
return stat | |
def divisible_3(x): | |
return stat_transfer_iteration(0, x) == 0 | |
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