 I'm ready to begin. Hello everyone, thank you for coming to my talk. I am Matthew, and I'm going to be telling you four stories about recreational maths. This is kind of like four talks I've given at shorter events, kind of crammed together into one longer talk, to keep you entertained for a full half an hour. First a little bit about me. I am a PhD student at UCL. Some of you might have seen me last time when I was just starting my PhD, I'm now two years in. I work on numeric analysis, so I work with PDs. If you are interested in acoustics or electromagnetics, talk to me afterwards. I work on this bit of software called Ben++, which is really nice. We can do really cool 3D simulations, but in reality I spend most of my time doing things like this, where I have simulated an electromagnetic wave bouncing off the EMF cap logo, because I didn't feel like working that day. On to the point of today's talk. The first story I'm going to tell you is about menace. Recently, machine learning has been in the news an awful lot with machines finally beating go. It's a big talking point, but a lot of the time you don't really get told how machine learning actually works. I'm going to explain to you one very simple aspect of machine learning, and tell you how to actually teach a computer to learn something. I'm going to explain it by explaining the machine built in the 1960s by this man, Donald Witschee. Before the 60s, he'd worked with Alan Turing at Splechley Park during the war, where we were two years ago. After that, in the 60s, he became interested in biology and machines and learning, and he built a machine called menace. This is a photo of his machine, menace. There are 304 matchboxes, which he has meticulously drawn, and noughts and crosses arrangement on each one, stuck little bits of carbon on each one, and filled each one with little tokens. Menace stands for machine-educable noughts and crosses engine. Brilliant acronym. This is his machine, which he designed, and it can learn to play noughts and crosses. How it works, I'm going to explain with an example game. The menace is going to play as noughts, and we're going to play as crosses. To start off, we find the box which has this position drawn on it. As I said, each box has a position drawn on it, and we find the box which has no hands on it. We shake this box, we open the box, and we find a red token at the front. A red token is code for going here, so menace has made its move. Now it's my move, and I decide to go here. Then again, it's menace's move, so then we find the box which has this design drawn on it, so it will be that box. We shake that box, we open the box, and we find a blue token, and a blue token is code for going here. Now it's my turn again, I go here, I'm at this point quite happy with myself, I seem to be doing quite well. It's menace's turn, so I find the box again with that on it. Shake the box, open the box, and a green token. The green token is code for going here, so it's blocked me. My move, I'm now very, very happy with myself. Then it's menace's turn, I find that box, shake the box, open it, find a yellow token, and yellow is code for going there, and I go there, and I've won. So this is a game which is early on before it's done much learning. It's not very good, and I've just destroyed it. But here is where the learning happens, and this is how the machine learning works. I take that token out of the box, which means this was probably a bad move because it led to losing. It's now less likely to do that. I take that token away, it's less likely to go there. I take these tokens away, and it's less likely to make the mistakes it made again. If you play about 50 games against this, it gets pretty good at drawing with you, just by those simple mechanics. You should also, as well as losing, if you draw, you add one bead, and if you win, you add three beads. So you're encouraging it to do more of the things it led to winning, discouraging it to do things led from losing. This matchbox machine, it actually does particularly well. This is the graph of Donald's tournament with it. So we spent a whole weekend playing 240 games against this thing, carefully adding and taking away tokens, and plotting out what happened. So this is starting at zero here. It is the change in the number of tokens he's got. So you can see it started off by losing lots of games for the amount of tokens decreased, and then it got pretty good at learning here and increased. And then each of these is where he changed his strategy. So you can see here he changed his new strategy, which you hadn't seen before, so it doesn't know how to win yet, but after about 20 games, it picks up, it's worked out to beat him on that strategy, and it carries on like that. So this is how to teach 304 matchboxes how to play Norse and Crosses. And if you want to play against it, you can go to this page on my website where I've built a JavaScript version of it to save you the time of building one yourself. Alternatively, you could go away and build your own copy of it, which is what Oliver just here did when I did this talk at CCC. He got his matchboxes, printed all of these, which all have the positions in them, filled all up, laser cut a brilliant box for it. But if you have any ideas what to do with them, I'm sure you'd like to hear from you because did you find anything to do with them? Oh, nice. So he's approximated Pi by dropping them on parallel lines because a large number of straight sticks will do that for you. Brilliant. Right, on to story number two. This is about a Twitter bot I created about two years ago called MathsLogic Bot, so it says, I tweet logical tautologies. I'm currently working my way through propositional calculus. You can read our book here. And so it does these brilliant, really interesting tweets like this. So I'm going to explain to you what I mean by logical tautologies and propositional calculus and what this bot is actually doing. So this is part... propositional calculus, it's a form of logic in Maths. So it's a way of writing sentences and then analysing these and analysing the system of logic itself rather than just using logic to prove things. And so you build up these sentences in it from certain symbols. And these are the symbols we are allowed to use. So we're allowed to use variables. So I'm going to use the letters A to Z and then carry on with Greek letters if I need any more as variables. So they can be things which are set to be either true or false. I'm going to use not. This symbol like that means not. This symbol means implies. This symbol means if and only if or it's kind of like is equal to. This is and and this is all and I'm going to use brackets. And these are the only symbols I'm allowed to use in this story. And they're helpful on the side of the slide if you need to look up what they mean. And from these symbols we're allowed to build what are called formulae. And these are kind of strings of symbols that make sense together. We have three open brackets because that is nonsense, it doesn't work. So we have rules, we're allowed to build formulae and there's three rules. The first rule is that every variable is a formula. The second rule is that if I've got a formula then doing not that formula that's still a formula. And if I've got two formulae then I can put any of these between them and wrap them in brackets. So for example A is a formula because of rule one. A implies A is a formula and rule three because I've taken two copies of that one and put an arrow between them. That is a formula by... B is a formula by the top one. This is a formula and put them together by the third rule, you get that. And lots of other things like that are formulas. You can combine these symbols and it doesn't really matter what they mean but they make sense because if you read them out as using these you can have A implies A. You have B if and only if A implies A. It can make sense and you can work out kind of the truth of the sentence. So what the bot is doing is it's tweeting the tautologies and tautologies are formulae that are true whatever the values of the variables. For example A or not A because if A is true then that says true or not true. So then true or not true is like true or false and like one of true and false is true. And if A is false it says false or not false so it's like false or true. So it makes sense. It's got to be or not to be. One of them has to be true. Similarly A if and only if A is true because if and only if is very similar to the equal sign so it's something like of course A is equal to A. That's a tautology. However A and B is not a tautology because if A and B are both false then that's obviously not going to become out true. So that is not a tautology. So which is interesting is that in the sentences in this language that are true whatever we do with these variables. Okay so I decided to try and make a Twitter bot that tweets all of these that are less than 140 characters long. So very briefly how this works is I started by running through all possible strings of symbols. So forgetting about formally for now just running through them like this. So I did all the strings which are one character long then start with on the ones two characters long and then for each of these I first check that it's actually a formula. So at this point I can get rid of almost everything on the board because one and two character long things are almost certainly not formulae. So once I've got that so I'm running through all the formulae and checking they're valid and then I try all the different truth assignments so for the first one if A is false if A is true is it true and if it is always true I tweet it and you get lovely tweets like this. So this is one that Totology did back in April which I'm not going to attempt to read out now but you can believe me that that is always going to be true whatever the values of A and B and maybe you can sit down there for us and work out why. Brilliantly for a while Twitter had this automatic translate function here so if you clicked on that brilliantly it realised it was in Hungarian and what I was actually trying to say was the arrow A and all that garbage which was very helpful of Twitter so I've got rid of that feature now for no apparent reason. Okay so the first 18 Totologies are these and so there's a view I've been through there they seem pretty reasonable the first one's like A is the same as A if A is true then A is true either A or not A are true or A or not A are true and then some more and at this point I began to wonder how many Totologies there were going to be of each length so you can see that there's two that have five characters there's two that have six characters there's quite a lot more that have seven characters there's more that have this many characters and I started wondering what this sequence of the number of Totologies would look like so I added it to the Online Psychopathic of Intersequences which if you haven't heard of it it's a really really great online tool if you're ever trying to solve some problems like during my PhD it comes up a lot you get some numbers coming out you type them there and you can almost certainly find out what the sequence is and there's loads of links to facts about it so this sequence I started trying to think about some properties this sequence might have and I realised a few things firstly if you've got a Totology then not not that Totology is also a Totology it's like if something's true then not not that thing must also be true because the knots kind of cancel each other out which means the number of Totologies of length n plus 2 is going to be bigger than the number of Totologies of length n because for every Totology of length n we can make another one with two knots in front so there's got to be more of them that are two strings and symbols longer similarly you can get lots of situations like this where you've got a Totology and you say something doesn't matter or this thing which we know is always true so you get that the number of Totologies of length n plus 4 has also got to be bigger the number of Totologies of length n so it looks like as we get these strings longer and longer we're going to be getting more and more Totologies so I made the conjecture that it's going to be increasing so whenever we have another character we're going to get more Totologies but it turns out it's completely not true because the next one is 6 so it already decreases but carrying on from there we get 57, 88 373, 554 2086 and so on and so it looks like maybe that 6 is just a weird blip because our number of symbols is quite short and maybe it does increase forever so my modified conjecture is for n bigger than 8 it's going to be increasing but I have no idea if this is true or not and no real idea how to prove this so if anyone has any ideas I'd love to talk to you afterwards about how we might go about seeing if this is true or not brilliant my third story I'm doing great for time is about optimal Pac-Man so this is an idea I actually had at the last EMF camp when I was in the arcade tent I was playing Pac-Man and I got to wondering which route I should actually take on Pac-Man because it's not obvious where's the best direction to go at all points and this is one I got down to sitting down a few weeks afterwards and working out what I should do in Pac-Man so I was ready to come here and be better at Pac-Man it reminded me of a few problems and there's some bits of maths I used so the first problem it reminded me of is oh I should say Pac-Man you want to know what Pac-Man is so this is the map you've got here and find the route in this is the difficult question I was thinking about and it reminded me of this problem this is a problem from the 1700s which was solved by this man Leonard Euler and led to the whole creation of the field of graph theory and this is a map of Königsberg which is not very well drawn because I drew it myself where you had this town with four islands and these seven bridges going between the islands and the townsfolk of Königsberg used to play a really fun game where they'd walk around these islands and try and cross each bridge once and exactly once so for example you'd go over there and keep going around this way and you're stuck and you've missed a bridge so they wanted to find a route to go over every bridge to see lots of the town but they didn't want to go back in themselves because that's really boring and they found this really really hard to do but kept trying anyway until Leonard Euler came along and he worked out why they'd been finding it so hard because in fact it was impossible and he explained why this was impossible and he did this first by simplifying the map you've got down to what he called a graph like this so these are exactly the same diagrams but here I've shrunk each time down to a dot I just put lines in because we don't care what the map looks like we just care how the bridges connect the islands and this is what mathematicians confusingly call a graph and not like the X and Y coordinates things we call these graphs and then he had the real important thing he noticed here was that so we're trying to look for a route which goes up all these bridges once but to find that route if you think about walking around this island if I'm walking down here and I arrive at this island I need to leave this island again and I need to leave this island again so every time I come into one of these islands I'm going to have to leave again which means it's only possible to do this route if I compare at the edges at each vertex so here we're going to have a problem because if we come in on one and go out on another we've got this one out which eventually if we come in on here we're stuck so it turns out the important thing about this problem that he solved is that if you've got any edges with an odd number of bridges any islands with an odd number of bridges it's going to be impossible and so this is impossible because you've got three islands there, three bridges there three bridges there, three bridges there and five bridges there so every island you're going to get stuck at so it's impossible to find one of these routes okay so cool we've done this with a 300 year old problem how about Pacman so if you take Pacman and make it into a graph you get this graph here so I've just taken each of the points where multiple routes meet such as here and here I've made it into one of these points and I've replaced all of the curvy edges with lines because I don't care about distances and how it's arranged I just care about how things are connected and each of the nodes in red have an odd number of edges so we have a lot of odd number of edges we have a lot of islands that have an odd number of edges so we definitely can't do this so it's impossible to complete Pacman without looping back in yourself which is not surprising because if any of you have played Pacman you will know it's impossible to complete Pacman without looping back in yourself because you can't do it it's really hard to do because it's impossible so I've not actually solved the problem yet though so I've used this 300 year old thing and found that it's impossible so we want to do something better now and work out how should I be doing Pacman because I've got to do some repeating so this is a different problem related to the first one called the Chinese Postman problem where so the Chinese Postman problem is you've got a postman who needs to go down all the rows to deliver letters and he wants to do that by walking the shortest distance possible so it's a slight extension of the last one like instead of just not going back in yourself it's if you have to go back in yourself what's the least amount of going back in yourself that you have to do and the way you can go about solving this going back in yourself is a bit like building a copy of the bridge right next to the first bridge so for example on Königsberg you could imagine two extra bridges there which are really just repetitions of this bridge and this bridge and now by adding these lines everything has an even number of edges and so I could do a route around this or I could have added these edges here and again made everything even or I could have added those three edges there and made everything even we're interested in the shortest way of doing this so looking at these three options if we add up the total length of those two bridges add up the length of those two bridges and add up the length of those three bridges whichever those add up to the least that's going to be the shortest way of doing this path going over all the different bridges so we need to look at all the combinations of matching up these odd edges for Pac-Man it turns out this is the shortest pairings for Pac-Man and with this number of edges it took my computer all night to solve this because as you add edges the number of possible ways of pairing up increases really really quickly so the Pac-Man map was much bigger this would be almost impossible to solve because it gets out of hand really really fast but luckily for Pac-Man we managed to do this so these are the edges I want to repeat so I got very good at Pac-Man the day after I worked this out and there should be some sound coming on now this is my attempt at Pac-Man it's eerily quiet so I've repeated that edge then I go around here and I've repeated that edge no I haven't, I've gone over that one I've repeated that edge there and I'm going up the top so there's that edge repeated and that edge repeated and that one and that one another one repeated so I need to repeat that one down there next next is there and then finally go back and fill in the one I hadn't repeated over there and that is the quickest possible way to complete Pac-Man and of course then it starts all over again and I couldn't be bothered to do it more than once in a row so I stopped the video there okay so my final story which is actually more of a puzzle than a story so I thought it'd be fun to send you away with something to go and think about and something to do so this is a few puzzles I've been thinking about recently that I've been working on I've got a few ideas how to solve some of them and not others so this is something to keep you entertained this evening when you get bored of going to all the workshops which will never happen because there's so much to do here so this is a puzzle first of all taken from Starting Points by Bamwell's Swords and Tata which is a really good teaching book full of kind of investigatory maths and the question in this book is you take a rectangle like this 4x3 rectangle here and you fire a ball across at 45 degrees so that it goes down here carries on bounces off the side and carries on eventually it ends up in the top right corner so for a 4x3 rectangle the bouncing ball ends up at the top right corner so the puzzle is for different sized rectangles what corner will the line end up in and I'm not going to tell you I'm going to leave you with that to go away and think about because it's really fun to work out but it's not that difficult you can work this out in about 20 minutes you can get a good answer and work out why that would be but from this there's a few related puzzles I got wondering about so the first thing I thought was how many squares does the line go through on its way so in the 4x3 rectangle there's a diagonal line going through every single of one of the smaller squares but for different sizes for example we've got a 3x6 here we only go through those squares so when will the line go through all the squares is another question and this is one I haven't actually worked out the answer to yet so I'm probably going to work this out myself this evening if I can and a third question is how many corners does it go through so as the lines going round on the 4x3 rectangle it goes through those corners but if you take a general rectangle how could you count the number of corners it goes through which is another problem to pose I have solved this one I was very happy with the way I worked this one out and when will it go through all the corners and this is related to there's a problem called the cross-dangle cover problem by Garish Corpor which he blogged about here he was in a maths lecture got a bit bored and started doodling on rectangles so he got his rectangle and he started drawing crosses going in a diagonal way so fairly similar to what I was doing he did some crosses going like this and bouncing around the rectangle like that and eventually reached another corner so kind of similar to what I was doing but a little bit different and then he asked the question how many of the squares ended up with crosses inside them and I was very happy when I found this on the internet because it turns out this is exactly the same as that rectangle here is one square larger than that one but if you connect up the middle points of that then the pattern I just drew across is exactly the same as the pattern I have on my rectangle up there so his problem is kind of a way of looking at my problem upside down so by having solved that I managed to solve his problem so that is three fun puzzles for you to work on this evening and let me know if you find some answers because it would be good to confirm I'm right and find out answers for the ones I haven't yet solved but for now this is a clue so when I said I was bouncing around the rectangle you couldn't imagine that you've actually got mirrors here and the ball is going straight down that will really help that immediately solves two of the problems so that is a clue for anyone who wants to go and work on this but for now that is all I'm going to talk to you for now so I'll go to the bar now and you can come and chat with me and ask some questions thank you for listening