Copyright © University of Cambridge. All rights reserved.

'Funny Factorisation' printed from https://nrich.maths.org/

Show menu

Some 4 digit numbers can be written as the product of a 3 digit number and a 2 digit number using the digits 1 to 9 each once and only once.

Well done Sally Nelson and Sarah Dunn, S2, Madras College, St Andrew's for finding altogether six funny factorisations, but there is one more. It is now a Tough Nut to find the last one. You might like to write a computer program to find all seven funny factorisations or you might come up with a different method. Let us know.

The number 4396 = 2 x 2 x 7 x 157 and there are not many possible combinations. By trial and error we get 4396 = 28 x 157.

The number 5796 = 2 x 2 x 3 x 3 x 7 x 23.
So 5796 = (2 x 3 x 7) x ( 2 x 3 x 23) or (2 x 2 x 3) x (3 x 7 x 23) amongst other possibilities which don't turn out to be 'funny'.
In this way we find the two funny factorisations: 5796 = 42 x 138 and 5796 = 12 x 483.

Similarly 5346 = 2 x 3 5 x 11 and the funny factorisations are:
5346 = 27 x 198 and 5346 =18 x 297.
 

Here you must use the digits 1 to 9 once, but only once, to replace the stars and complete this multiplication example.

  * * 9
$\times$   4 *
--- --- --- ---
* 6 * *

 

Firstly I found out the possible solutions for the top row. It could not be a number above 250 or below 100 and it had to end in a 9. The number could not have a 4 or a 6 or another 9. The only possibilities were 129, 139, 159, 179, 189, 219 and 239. So I tried these numbers with every 2 digit number beginning with a 4 until I found the answer 159 x 48 = 7632.

Extension
We asked you to find all of the funny factorisations of 4-digit numbers which use all of the digits 1 to 9. We received two Python programs, one from Chris from Belmont, Massachusetts, USA and one from Ryan. Ryan's code sticks to the original problem, but Chris worked on a further extension. Chris wrote:

I did the activity and then I got curious, what if there are funny factorizations with 3 digits times 2 digits equals 5 digits? So I wrote some python code. The program starts by making a empty list and then the for loops are for sifting through every possibility. Inside the for loops it adds all the digits of the numbers (and their multiple) in the list. It then sorts the list and checks if every number in the list is unique. If so it prints out the funny factorization. 

This is the output of Chris's code. Notice that the digits 0 to 9 are now used:

297x54=16038 345x78=26910 367x52=19084 
396x45=17820 402x39=15678 495x36=17820 
594x27=16038 715x46=32890 927x63=58401

Click to see Chris's code. Can you explain what each line does?

for i in range (100,1000):
    for j in range (10,100):
        list1=[]
        if(len(str(i*j))==5):
            list1.append(int(str(i)[0]))
            list1.append(int(str(i)[1]))
            list1.append(int(str(i)[2]))
            list1.append(int(str(j)[0]))
            list1.append(int(str(j)[1]))
            list1.append(int(str(i*j)[0]))
            list1.append(int(str(i*j)[1]))
            list1.append(int(str(i*j)[2]))
            list1.append(int(str(i*j)[3]))
            list1.append(int(str(i*j)[4]))
            list1.sort()
            if (list1 == list(set(list1))):
                print(str(i)+"x"+str(j)+"="+str(i*j))

Click to see an explanation. Could you think of any ways to make the code more efficient?

Chris uses i to represent the three digit number and j to represent the two digit number. From the first two lines of code, we can see that Chris's program goes through every possible three digit number (i can be between 100 and 1000), and, for every possible three digit number, it goes through all the possible two digit numbers (j can be between 10 and 100). That's a lot of calculations!

Then, for each possible i and j (or each possible two digit number and three digit number combination), Chris creates an empty list called list1. This is really a space to store information, like a blank piece of paper.

Chris's program then calculates i*j (i multiplied by j), and checks whether it has 5 digits. If it does, then all of the lines of code beginning "list1.append..." are executed. If i*j doesn't have 5 digits, then it moves straight on to the next i and j.

Assuming i*j has 5 digits, Chris's program then "appends" all of those 5 digits to the list - like writing them down on a piece of paper. It also put the individual digits in i and j in the list (in fact it puts those on first). It then sorts the list in ascending order. Then, Chris needs to check whether there are any duplicates on the list. He does this by turning it into a "set" - which is free of duplicates - and then turning it back into a list so that it can be compared with the original list. If the program finds that removing duplicates has not changed the list, then it prints out the funny factorisation before moving on to the next i and j.

Ryan's code works in a different way - and notice that Ryan's final numbers are still 4 digits long. Click to see Ryan's code. Can you explain how it works?

for j in range(9):
    L=[1,2,3,4,5,6,7,8,9]
    a=0
    a=L[j]
    M=L[:]
    M.pop(j)
    for k in range(8):
        b=0
        b=a+10*M[k]
        N=M[:]
        N.pop(k)
        for l in range(7):
            c=0
            c=N[l]
            O=N[:]
            O.pop(l)
            for m in range(6):
                d=0
                d=c+10*O[m]
                P=O[:]
                P.pop(m)
                for n in range(5):
                    e=0
                    e=d+100*P[n]
                    f=b*e
                    Q=P[:]
                    Q.pop(n)
                    for o in range(4):
                        f=0
                        f=Q[o]
                        R=Q[:]
                        R.pop(o)
                        for p in range(3):
                            g=0
                            g=f+10*R[p]
                            S=R[:]
                            S.pop(p)
                            for q in range(2):
                                h=0
                                h=g+100*S[q]
                                T=S[:]
                                T.pop(q)
                                for r in range(1):
                                    i=h+1000*T[r]
                                    if b*e==i:
                                        print b,e,i

Click to see an explanation. Could you think of any ways to make the code more efficient?

Ryan begins with the second digit of the two-digit number, which can be any of the 9 digits in L, so the program runs through 9 values of i. These values, in range(9), are 0 to 8, because Python starts counting from 0. For this reason, Ryan's program defines L as a list containing the possible digits, 1 to 9, and then defines a as the ith element of L - so when i is 0, a is the 0th element of L which is 1, and so on until i is 8 and a is 9.

For each value of a, which will represent the second digit of the two-digit number, Ryan's program then makes another list M which begins life the same as L (using the code M=L[:]). Ryan then uses M.pop(j) to remove the jth element of M from the list - which was a. So M is the list of possible digits for the first digit of the two-digit number, which doesn't contain a, because a is the last digit.

There are only 8 digits in M, so for each value of a, there are 8 options for the two-digit number. Ryan's program lets k run from 0 to 7 and chooses the kth digit from M to create the two-digit number, which is called b and comes from doing a plus 10 times the kth digit in M.

Ryan's program then makes a copy of the list M (called N) to store the possible numbers that the third digit of the three-digit number could be. It removes the kth digit from N, because that one shouldn't appear again. This process continues - all possible three-digit number are chosen using all only digits that are not in the two-digit number. Then, for each two- and three-digit number pair with no digits in common, all possible four-digit numbers are checked using the remaining four digits.

Finally, for each two-digit, three-digit and four-digit number combination which use the digits 1 to 9, Ryan's program checks if the four-digit number is the product of the other two - and prints it if it is.

Which program do you find easier to understand? Could Ryan and Chris have used comments to make their code more human-readable? Which is more similar to your method? Which do you think is more efficient for a computer to run?