0

下面是我的脚本:

from math import trunc

def solver(number,number2, numberofdigits):
    seq = str(number),str(number2), str(number - number2)
    digits = "".join(seq)
    goodChecks = 0
    count= numberofdigits/3
    for i in range(1,10):
        if digits.count(str(i)) == count:
            goodChecks += 1
    if goodChecks == 9:
        return digits
    else:
        return False


middlenumberdic = {}
middlenumber =[]
successes = 0
num_of_digits = int(input("please enter a number of digits, which is a multiple of 3"))
if num_of_digits == 3:
    minY = 381
    maxY = 987
if num_of_digits == 6:
    minY =246912
    maxY = 998877

if num_of_digits == 3:
    minX = 123
if num_of_digits == 6:
    minX =123123


for y in range(minY, maxY+1):
    numberlist = []
    if y%100 == 0:
        print(y)
    for x in range(minX,trunc(y/2)):
        digits = solver(y,x,num_of_digits)
        if digits is not False:
            successes += 2
            print(digits)
            numberlist.extend([x,y-x])
            middlenumber.extend([x, y-x])

print("")
print("I found: ", successes, " successful solution to your brainteaser")
if successes < 20:
    print("there were almost no solutions")
elif successes < 100:
    print("there were not many solutions")
elif successes < 1000:
    print("there were more than a hundred solutions it is definitely not impossible :)")
else:
    print("that's a lot of successes")

print("All the ", successes, " succesful solutions i am now going to show you :)")
print("There were ", len(middlenumber) - len(set(middlenumber)) , " duplicates, by the way :)")

items = sorted(middlenumberdic.items())
for key, value in items :
    if not not value:
        print(key, " : ", ", ".join( repr(e) for e in value ))

所以我创造了一个脑筋急转弯,我称之为“不可能的问题”。在这个脑筋急转弯中,目的是创建一个有效的 3 位减法,它使用从 1 到 9 的每个数字 以下是其中一种解决方案的示例: 873-254=619 这是有效的,因为每个数字都使用一次。

有关更多信息,请观看我制作的视频:https ://www.youtube.com/watch?v=-2i1nOy6mfo&ab_channel=EpicVideos

在发现很难想出答案后,我创建了这个程序。它的基本作用是遍历每一个可能的 3 位减法,如果找到符合标准的减法,它就会打印出来。

我的程序运行得很好,但后来我决定你可以对 6 位数做同样的事情吗?对于 3 位数的问题,程序有 9^6 种可能进行迭代。这是微不足道的 531,441 次迭代。但是对于 6 位数字,有 9^12 的可能性,即 282,429,536,481 次迭代。这需要我的电脑几天才能解决。

我已经尝试过优化我的程序,但我不知道如何更快地做到这一点。因此,如果您在任何时候看到有一种方法可以优化它,请告诉我。谢谢

4

0 回答 0