0

Below is my script:

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 ))

So i have created a brainteaser, which i called the "impossible problem". In this brainteaser, the aim was to create a valid 3 digit subtraction which used every number from 1 to 9 Here is an example of one of the solutions: 873-254=619 this works because every number is used once.

For more information watch this video i made: https://www.youtube.com/watch?v=-2i1nOy6mfo&ab_channel=EpicVideos

After finding it hard to come up with answers i created this program. What it basically does is iterate through every possible 3 digit subtraction and if it finds one which fits the criteria it prints it.

My program worked quite well but then i decided could you do the same for 6 digits? For the 3 digit problem the program had 9^6 possiblitys to iterate over. Which is a measly 531,441 iterations. However for the 6 digit there is 9^12 possiblity, Which is a collosal 282,429,536,481 iterations. This is going to take my computer days to solve.

I have already tried optimizing my program but i can't figure out how to do it any faster. So if you see that at any point there is a way i could optimize it, please could you tell me. Thankyou

Joe Clinton
  • 155
  • 9
  • 3
    I believe this is an excellent candidate question for http://codereview.stackexchange.com/ – blue Mar 24 '16 at 19:36
  • I have not used this yet but i will upload it there, if it fits the purpose better. Thankyou. Should i remove this post then? – Joe Clinton Mar 24 '16 at 19:38
  • to address your question directly, you could split up the range [minY, maxY] in, say, 5 subranges and apply a Pool.map to iterate over the different subranges in parallel. At a glance I cannot think of other trivial optimization (apart from switching to C obviously, which would make your code much faster and should not require much effort in translation, if you know C a little bit) – blue Mar 24 '16 at 19:40
  • @JoeClinton if you post it to Code Review please take a moment to edit the title to say what the code does, as improving code is implicit there. There is also a [performance] tag on Code Review for that purpose. – Phrancis Mar 24 '16 at 19:46
  • Thankyou, i will do this – Joe Clinton Mar 24 '16 at 19:48

0 Answers0