Last active
February 5, 2017 22:12
-
-
Save ilyakamens/da35adcff98727cf3c77842d2941cf0f to your computer and use it in GitHub Desktop.
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
##### | |
# The sum of the middle two digits is a Square Number. | |
# The sum of the middle four digits is a Cube number | |
# The net difference between the first and tenth digit is 2 | |
# The net difference between the second and ninth digit is 3 | |
# The net difference between the third and eighth digit is 4 | |
# The sum of the first five digits is a Prime number | |
# The sum of the last five digits is a Triangular number | |
# The sum of the digits in odd positions (first, third, fifth, seventh and ninth digits) is an Odd number | |
# The digit 4 is in first 5 positions | |
##### | |
def check1(code): | |
return code[4] + code[5] in {1, 4, 9, 16} | |
def check2(code): | |
return code[3] + code[4] + code[5] + code[6] in {1, 8, 27} | |
def check3(code): | |
return abs(code[0] - code[9]) == 2 | |
def check4(code): | |
return abs(code[1] - code[8]) == 3 | |
def check5(code): | |
return abs(code[2] - code[7]) == 4 | |
def check6(code): | |
return (code[0] + code[1] + code[2] + code[3] + code[4]) in {5, 7, 11, 13, 17, 19, 23, 29, 31} | |
def check7(code): | |
return (code[5] + code[6] + code[7] + code[8] + code[9]) in {6, 10, 15, 21, 28} | |
def check8(code): | |
return (code[0] + code[2] + code[4] + code[6] + code[8]) % 2 != 0 | |
def check9(code): | |
return 4 in {code[0], code[1], code[2], code[3], code[4]} | |
def permute(code): | |
if len(code) == 2: | |
return [code, code[1] + code[0]] | |
codes = [] | |
new_code = code | |
while True: | |
codes += map(lambda p: new_code[0] + p, permute(new_code[1:])) | |
new_code = new_code[1:] + new_code[0] | |
if new_code == code: | |
break | |
return codes | |
def check(code): | |
return (check1(code) and | |
check2(code) and | |
check3(code) and | |
check4(code) and | |
check5(code) and | |
check6(code) and | |
check7(code) and | |
check8(code) and | |
check9(code)) | |
if __name__ == '__main__': | |
i = 0 | |
for code in permute('0123456789'): | |
if i % 100000 == 0: | |
print code | |
i += 1 | |
if check(map(int, code)): | |
print 'code found: %s' % code | |
exit(0) | |
print 'no code found' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment