Skip to content

Instantly share code, notes, and snippets.

@yasuharu519
Created July 26, 2011 17:00
Show Gist options
  • Save yasuharu519/1107226 to your computer and use it in GitHub Desktop.
Save yasuharu519/1107226 to your computer and use it in GitHub Desktop.
1~9の数を一度だけ使って、□□□□ × □ = □□□□ を満たす式を考える
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# □□□□ × □ = □□□□
# として、□の中に1~9の数字が一つずつ入る時の組合せ
# 1000~9999 * 1~9 を総当りで確かめてみるか → パソコンの力任せ
# (a, b, c) で組合せを表現
# 3値のタプルを入力とし、使われている数字をリストで返す
def check_used_number(number_set):
"""
>>> check_used_number((1234, 55, 66))
[1, 2, 3, 4, 5, 5, 6, 6]
>>> check_used_number((2222, 45, 89))
[2, 2, 2, 2, 4, 5, 8, 9]
"""
output_list = []
for num in number_set:
for char in str(num):
output_list.append(int(char))
return sorted(output_list)
# 入力した数で数字が重複しているか調べる
def is_duplicated(num):
"""
>>> is_duplicated(1111)
True
>>> is_duplicated(1234)
False
>>> is_duplicated(1223)
True
"""
used_num = []
for n in str(num):
if int(n) in used_num:
return True
else:
used_num.append(int(n))
return False
def main():
# 1000 から 9999 までの数を作ってセットにしておく
num_set = [tuple([x, a, a * x]) for x in range(1000, 10000) for a in range(1, 10) if is_duplicated(x) is False and is_duplicated(a * x) is False]
for num in num_set:
if check_used_number(num) == range(1, 10):
print num
# doctest用
def _test():
import doctest
doctest.testmod()
if __name__ == '__main__':
import sys
if len(sys.argv) > 1:
if sys.argv[1] == "test":
_test()
else:
print "if you want to run doctest, you have to type test"
else:
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment