女王的数字问题
发表于 : 2023年 2月 3日 19:53
女王问了一个数字和乘法的问题。拿1到9这九个数字,分成两组,每组的数字排成一个数。如果把这两个数,相乘后得到一个九位数,也由1到9这九个数字组成。这样的解有哪些?
这个总没有规律了吧。
如果没有规律,就穷举吧。看着很容易就穷举完了。
这个总没有规律了吧。
如果没有规律,就穷举吧。看着很容易就穷举完了。
不能重复
给出一个例子?长啥样?meiyoumajia 写了: 2023年 2月 4日 22:44 女王为啥要大毁子民的人生?
两乘数中小者在2与31426之间
单位数:8个
2位:9*8
3: 9*8*7
4: 9 *8*7*6
5: 2*8*7*6*5 + 2*6*5 + 5 + 2
共7035个
用它们去除123456789等共362880个数
2552860800次运算
25.5亿
把女王推翻,行不?
没想到这么大数字,怪不得我算了好一会儿也没算完,只好Ctrl-C了
为啥要“除123456789等共362880个数”, 乘完检查一下数字不就行了。meiyoumajia 写了: 2023年 2月 4日 22:44 女王为啥要大毁子民的人生?
两乘数中小者在2与31426之间
单位数:8个
2位:9*8
3: 9*8*7
4: 9 *8*7*6
5: 2*8*7*6*5 + 2*6*5 + 5 + 2
共7035个
用它们去除123456789等共362880个数
2552860800次运算
25.5亿
把女王推翻,行不?
我就是这样写的程序。那说明还是可以算的,让我再算算看。
有很多解 1279个,而且似乎有些数字,排列组合之后还是成立
import itertools a = "123456789" def test_num(x): if x > 987654321 or x < 1234567: return False x = str(x) if "0" in x: return False b = set([c for c in x]) return len(b) == 9 sol = [] for s in itertools.permutations(a): s1 = "".join(s) for i in range(1, 4): c = int(s1[0:i]) d = int(s1[i:]) if (c > d): continue e = c * d if (test_num(e)): print ("{} * {} = {}".format(c, d, e)) sol.append((c, d, e))
你这个check的逻辑很好,比我的好(我用的是看是不是在那个permutation 后的set 里面)Caravel 写了: 2023年 2月 5日 07:34 有很多解 1279个,而且似乎有些数字,排列组合之后还是成立
9 * 85347261 = 768125349
9 * 85372461 = 768352149
9 * 85436127 = 768925143
9 * 85461372 = 769152348
9 * 85473126 = 769258134
9861 * 23475 = 231486975
9864 * 13752 = 135649728
9864 * 17532 = 172935648
9864 * 25137 = 247951368
9864 * 52137 = 514279368
9 * 87146325 = 784316925
9 * 87325146 = 785926314
9873 * 54612 = 539184276
我的很笨的code
import itertools
a = "123456789"
def test_num(x):
if x > 987654321 or x < 1234567:
return False
x = str(x)
if "0" in x:
return False
b = set([c for c in x])
return len(b) == 9
sol = []
for s in itertools.permutations(a):
s1 = "".join(s)
for i in range(1, 4):
c = int(s1[0:i])
d = int(s1[i:])
if (c > d):
continue
e = c * d
if (test_num(e)):
print ("{} * {} = {}".format(c, d, e))
sol.append((c, d, e))
哦。不错。运行还挺快的。Caravel 写了: 2023年 2月 5日 07:34 有很多解 1279个,而且似乎有些数字,排列组合之后还是成立
9 * 85347261 = 768125349
9 * 85372461 = 768352149
9 * 85436127 = 768925143
9 * 85461372 = 769152348
9 * 85473126 = 769258134
9861 * 23475 = 231486975
9864 * 13752 = 135649728
9864 * 17532 = 172935648
9864 * 25137 = 247951368
9864 * 52137 = 514279368
9 * 87146325 = 784316925
9 * 87325146 = 785926314
9873 * 54612 = 539184276
我的很笨的code
import itertools
a = "123456789"
def test_num(x):
if x > 987654321 or x < 1234567:
return False
x = str(x)
if "0" in x:
return False
b = set([c for c in x])
return len(b) == 9
sol = []
for s in itertools.permutations(a):
s1 = "".join(s)
for i in range(1, 4):
c = int(s1[0:i])
d = int(s1[i:])
if (c > d):
continue
e = c * d
if (test_num(e)):
print ("{} * {} = {}".format(c, d, e))
sol.append((c, d, e))
把我code改一点就可以支持任意进制
算了多久?Caravel 写了: 2023年 2月 5日 07:34 有很多解 1279个,而且似乎有些数字,排列组合之后还是成立
9 * 85347261 = 768125349
9 * 85372461 = 768352149
9 * 85436127 = 768925143
9 * 85461372 = 769152348
9 * 85473126 = 769258134
9861 * 23475 = 231486975
9864 * 13752 = 135649728
9864 * 17532 = 172935648
9864 * 25137 = 247951368
9864 * 52137 = 514279368
9 * 87146325 = 784316925
9 * 87325146 = 785926314
9873 * 54612 = 539184276
我的很笨的code
import itertools
a = "123456789"
def test_num(x):
if x > 987654321 or x < 1234567:
return False
x = str(x)
if "0" in x:
return False
b = set([c for c in x])
return len(b) == 9
sol = []
for s in itertools.permutations(a):
s1 = "".join(s)
for i in range(1, 4):
c = int(s1[0:i])
d = int(s1[i:])
if (c > d):
continue
e = c * d
if (test_num(e)):
print ("{} * {} = {}".format(c, d, e))
sol.append((c, d, e))