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