The Problem¶

 -------------
 |  a  |  b  | -> 0.ab
 --------------
 |  c  |  d  | -> 0.cd
 -------------
    |     |
    v     v
   0.ac  0.bd

Given the above grid find an assignment of the digits $0-9$ to $a,b,c,d$ so that no digit is used twice and the sum is $1.0$ or the closest that that the assignments can get to it

Brute force: Just check all possible values¶

Note $a=0$, $b=1$, $c=2$, $d=3$ is different than $a=1$, $b=0$, $c=2$, $d=3$. In other word the same digits assigned in different locations is a unique "permutation" that must be checked but remember no digit can be repeated.

Permutations¶

We can figure out how many different assignments that we have to check. Since there at $10$ choice for one of the locations then $9$ remain for the second, $8$ for the third and $7$ for the last one. So $10 \times 9 \times 8 \times 7=5040$ is the total number of assignments.

Formally this is called the permutations of $4$ digits from a group of $10$ and this is the formula for calculating the number permutations = $10!/(10-4)!$ Where $!$ is called factorial and is defined as multiplying a number by all the numbers less than it until you get to $1$. Eg. $5! = 5 \times 4 \times 3 \times 2 \times 1$

See Permutations on Khan Academy for more information about Permutations.

A Solution¶

Write a program that check all possible assignments.

The following program searches all of the permutations and checks to see if any produce a sum to $1.0$ and prints the values for a,b,c,d

The Code¶

In [1]:
# Brute Force solution written in Python

# If you want to see all the permutaition tested and their sum set the 
# following variable to True
seeAll=False

print("Results:")
# The following code searches all of them
i=1    # counter for each permutation values for a, b, c and d that we try
for a in range(10):                        # loop a over 0-9
    for b in range(10):                    # loop b over 0-9
        if (b==a):                         # if b is equal to a then
            continue                       # skip this b and go on to next one
        for c in range(10):                # loop c over 0-9
            if (c==a or c==b):             # if c is equal to a or b then
                continue                   #  skip this c and go on to next one 
            for d in range(10):            # loop d over 0-9
                if (d==a or d==b or d==c): # if d is equal to a, b, or 
                    continue               # c skip and move on
                # We have an a, b, c, d to check
                #  a/10 = .a and  b/100 = 0.0b so a/10 + b/100 = .ab
                #  value = .ab   
                value=a/10 + b/100  
                #  a/10 = .a and  c/100 = 0.0c so a/10 + c/100 = .ac
                #  value = .ab + .ac 
                value+=a/10 + c/100 
                #  b/10 = .b and  d/100 = 0.0d so b/10 + d/100 = .bd
                #  value = .ab + .ac + .bd
                value+=b/10 + d/100 
                #  c/10 = .c and  d/100 = 0.0d so b/10 + d/100 = .cd
                #  value = .ab + .ac + .bd + .cd
                value+=c/10 + d/100 
                
                # check if .ab + .ac + .bd + .cd is equal to 1.0
                if (value==1.0): 
                    # yes then we found a solution print it out
                    print("* ", str(i) + ": " + str(a) + " " + str(b) + " " 
                          + str(c) + " " + str(d) + ": " + str(value))
                if (seeAll and value != 1.0):
                    # if we want to see all the others print them out too
                    print(str(i) + ": " + str(a) + " " + str(b) + " " 
                          + str(c) + " "+ str(d) + ": " + str(value))  
                    
                i=i+1                      # increase count of the values we 
                                           # have tried
Results:
*  40: 0 1 7 6: 1.0
*  137: 0 3 5 6: 1.0
*  242: 0 5 3 6: 1.0
*  341: 0 7 1 6: 1.0
*  537: 1 0 6 7: 1.0
*  579: 1 2 4 7: 1.0
*  684: 1 4 2 7: 1.0
*  789: 1 6 0 7: 1.0
*  1028: 2 0 4 8: 1.0
*  1077: 2 1 3 8: 1.0
*  1133: 2 3 1 8: 1.0
*  1526: 3 0 2 9: 1.0
*  1631: 3 2 0 9: 1.0