简体   繁体   中英

How do I check every possible combination for validity?

For context, I am trying to find all viable solutions to this question:

here's the code I have so far, but I am having trouble with the part that is supposed to iterate through every possible combination.

x = 1
y = 1
z = 10
a = 10

while x < 10 and y < 10 and z < 100 and a < 100: #iterates through every possible combination
    x = x + 1
    y = y + 1
    z = z + 1
    a = a + 1

    if x != y: #checks if x and are the same
            if a/x == z/y or z/x == a/y: #checks if x and y are proportional to a and z
                a = str(a) #converting each int to string
                z = str(z)
                x = str(x)
                y = str(y)
                if a.count(x) < 1 and a.count(y) < 1 and z.count(y) <1 and z.count(x) < 1: #checks if any number reapeats
                    print(x, y, z, a) #prints viable solution```

You have six boxes to fill. Just run through all permutations of 6 members and check the conditions:

import itertools

for a,b,c,d,e,f in itertools.permutations([0,1,2,3,4,5,6,7,8,9],6):
  if a == 0 or b == 0 or c == 0 or e == 0:
    continue
  if (10*c + d) / a == (10*e + f) / b:
    print( a, b, c*10+d, e*10+f )

It looks like there are 57 solutions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM