 If you're a bit of a nerd, you've probably heard of the collax conjecture, is a bit of mathematics, is supposed to be really simple to understand, but really, really difficult to prove, number file and a few others have done some really good videos on it. I don't want to get into the maths of it, I just want to show how to maybe do that calculation in a spreadsheet because it turns out that because it's recursive, a recursive lambda function is really a good way of dealing with it. And so it's a good tutorial for how to maybe put in a loop of some description if you want to get this blaster thing to do some coding. So what is collax conjecture? Well I'm going to start with a number and then we ask a question. If it is even, so if that number is even, what I want to do is take it and divide it by two. And if it isn't even, well I want to times it by three and plus one, so I get sixteen which is even, so it's going to half to eight, it's going to half to four, two to one. And what you see is when you hit one it begins to loop and maybe we could do different numbers and you can see the conjecture kind of states that everything should hit one and do that loop one back to four, two, one, four, two, one and so on. So sometimes these go on for a very long time and you might not know exactly when it stops, so we could do this and drag it down but this one on your 27 goes on to I think 111, 111 iterations to get it down to one. And this has been checked up to huge numbers, I would not recommend doing that in a spreadsheet, oh come on this is just a tutorial for how to do the lambdas. So we want to be able to loop this around as many times as we like. Now first thing, this is a little bit of an aside, this is even function. I'm going to write down a sequence for a second, I'm just going to put it in 10 rows. I'm going to ask is even, let's close that, false and we'd expect true, false, true, false, right, that works. Now if I put a hash here to make a dynamic range error, this is a little bit of a, it took me a while to figure out what was going on here. These functions are coded up in a particular way that only accept a number, they don't accept an array. You can override it if you simply put plus in front of it and it works. It's really weird because if we actually wrote, let me just copy that, actually not copy, type it. If I wrote out sequence 10 and put it in there, it also, it works. It just, it doesn't accept a dynamic array. It's really, really weird that it does that, let me just put it back in. It doesn't accept it, but I can overwrite it if I put plus because then it thinks that it's a number and it works and it spills down normally. With that in mind, we'll have to put that in. You could also, there's more than one way of doing it. You can do a mod function and see if that is equal to zero to work it out. You do it that way. What we need to do is figure out a lambda function. These are ways of creating short user defined variables. Do a very quick recap of how that works. Let's say we've got x and y are our variables and then we say x plus y. Calc doesn't really do anything, but afterwards we can put a new bracket in and define our variables x and y. In order to four, this should produce six. There we go. Now, if I then copy my whole lambda function, control C there, go to formulas of my name manager. I can just call this maybe add. Now I'm going to paste it in there. Close that. Now I have a function called add. It will tell you what you need based on what you told the lambda function. I've called it x and y here. It's going to execute x plus y. I can add it in once I've added it to the name branch. For recursively calling these, they're a little bit tricky. I think this is probably not necessarily a feature. I think this is just a hack on top of what the lambda is supposed to be doing, but it does work. Let's start with the lambda. What we need, I'm going to take an input. That's going to be whatever number I'm going to do the collat's conjecture on. Then I want to set up basically a for loop. If you've done coding in Python JavaScript, you'll be familiar with loops we want to do something a certain number of times. We don't want this to go on forever. We want a way out of it. I'm going to put i for an index and then a limit. These are the three inputs I want. I want to deal with a limit and then i, which will be a certain number, which will increase until we hit the limit. What is the function? First of all, we need to think about how we're going to escape the function to stop the loop. I'm going to do if i is equal to the limit. I'm going to write timed out just to tell us that we've hit the limit. That's false. I want another escape condition, which is if the input is equal to 1. This conjecture says everything should hit 1, so we're going to return something. Reached 1 after space and i, I'm going to put in. If that is not true, so if we aren't timed out, if we haven't reached 1, I'm going to call my function. I'm going to call a collapse. At this point, we need to look at the very beginning because this is what we are doing. What does the collapse conjecture ask us to do to the input? That's where we get our if statement back again. If is even, I'm going to plus the input. If that is true, then it will be input divided by 2. If not, 3 times the input plus 1. Let's close that if statement. I move on to the next thing. This is what we're doing to the input. Every time that we call the function, we will do this to the input. We also need to change i. That's our next thing. We're going to do i plus 1. Whatever we start with, we are going to increment it by 1. We just push whatever the limit is. Close a ton of these. We get the calculation error because it's trying to call something that doesn't exist yet. I'm going to copy that. If I go to the name manager, new, I'm going to call it collapse. This is what I wrote down earlier. I'm going to paste that in there. Hopefully, if I do, I'm going to put in number 5 here. If I call my collapse function, what is my input? Is that 5 that I wanted? This is the index. I'm going to start this from 0. If it's 1, it hasn't done anything, so it's going to return 0. The limit, I'm going to say, run it up to 10 times. If it hits the limit, it's done it 10 times. It will stop and just tell me it's timed out. I've reached 1 after 5 attempts. If I put 2 here, I've reached 1 after 1 loop through it. I've reached 2 times, I've reached 1 after 3. If I do 7, I've timed out. Let's me increase my limit. There you go. It takes 16 attempts to reach 1 from 7. If I put 7 over here, you can see 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16. There you go. I've hit 1 there. Obviously, 27. This one takes a long time. I know this is 111. If I increase my limit to 200, I've reached 1 after 111. Iterations. I'm going to go back to my name, manage it and clarify that a little bit. I've reached 1 after and I iterations. I'm going to put and iterations. There you go. Maybe I've timed out after space and limit. In fact, after I keep it consistent. If I put my limit back down to 50, timed out after 50, put up to 1,000, I've reached 1 after 111 iterations. Now, if I do a sequence, 10 rows, and I want to change this D5 to a hash, this will tell me which one after however many iterations. This has worked. Maybe I can send this to 100. I've done it quite quickly. Let's try 1,000. How long does that take? Maybe a couple of seconds, but it's all been done recursively. Once you get to large numbers, maybe we start running into some maths errors, but you should not be using this to test the super high numbers. This is not the right software for it. This is just a tutorial on lambdas. Let's recap that. The thing that we need is we need to know what the input is, what we're going to change. You need to initiate some kind of index for a loop, a limit to stop it, and then your function must start with if, and you need to kind of put the escapes there, so if i is the limit timed out. If we've reached the end, well, we'll put the output condition, and if all that falls, call your function again. So you need to know what your function is going to be called in advance and write it in, and we need to put in what we do to the input number, what we do to the index, which is incremented, and then the limit where we stop it. That is brief introduction to lambda functions and a rather silly little case to be to test it with.