Last active
October 6, 2015 23:37
-
-
Save jakab922/1ef6d3d1d1449cbd1c65 to your computer and use it in GitHub Desktop.
Solution for the 9 digit problem posed at -> http://csokavar.hu/blog/2010/04/problem-of-the-week-9-digit-problem/
This file contains hidden or 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
checkers = [ | |
lambda nums: False, | |
lambda nums: True, | |
lambda nums: nums[-1] % 2 == 0, | |
lambda nums: sum(nums) % 3 == 0, | |
lambda nums: (10 * nums[-2] + nums[-1]) % 4 == 0, | |
lambda nums: nums[-1] in [0, 5], | |
lambda nums: sum(nums) % 3 == 0 and nums[-1] % 2 == 0, | |
lambda nums: sum([10 ** i * nums[-(i + 1)] for i in xrange(len(nums))]) % 7 == 0, | |
lambda nums: sum([10 ** i * nums[-(i + 1)] for i in xrange(3)]) % 8 == 0, | |
lambda nums: sum(nums) % 9 == 0 | |
] | |
digits = set(xrange(1, 10)) | |
def convert(num_list): | |
return sum(map(lambda x: 10 ** (x[0] - 1) * x[1], zip(xrange(9, 0, -1), num_list))) | |
def check(num): | |
for i in xrange(9, 0, -1): | |
if num % i != 0: | |
return False | |
num /= 10 | |
return True | |
if __name__ == "__main__": | |
curr = [[]] | |
for i in xrange(1, 10): | |
next = [] | |
for c in curr: | |
for j in digits - set(c): | |
n = c[::] | |
n.append(j) | |
if checkers[i](n): | |
next.append(n) | |
curr = next | |
converted = map(convert, curr) | |
if not all(map(check, converted)): | |
print "Some numbers don't satisfy the criteria" | |
exit(-1) | |
print "There are %s such numbers" % len(curr) | |
print "The numbers are the following: " | |
for c in converted: | |
print c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment