 As I make this video, I respectfully acknowledge that I'm standing on the unceded traditional territory of the Comox First Nation, and I thank them for the use of their lands. This video is about a problem that showed up in the co-recursive slack channel, and it involves spiral matrices. What spiral matrices are is you start with one in the center, and then you go up and spiral around two, three, four, five, six, and you keep spiraling around, counting higher and higher until you finally get all the way out to 49. So it spirals around those where the values come from. So the question was, if you use a coordinate system where this one is at zero, zero, can you find the coordinates for all the other values you see there? So for instance, 14 would have the coordinate one, two, and then up one. So it would have the index to one. Seven would have the index minus one, minus one. So that's how it works. What we want to do is we want to provide to a function a value and then have the index return to you. And I use j to do this, and the number, the function I actually did, and in j we call functions verbs. Nouns are the things that we work with. Verbs work on nouns. It kind of makes sense in a weird kind of way. So I'll take spiral 29 and it says the coordinates of it are one, three. And so I go over one and up three, one, two, three, and I'm at 29. So my verb works and that now I'm going to explain how it works. And actually more importantly in a lot of ways, I'm going to explain how I came to think about it. So when I looked at this problem. A lot of other people went and generated all the indexes for all the numbers. And I thought, you know, I don't really have to generate all the indexes for all the numbers. If I know certain numbers in each square here, I'll be able to go relative to those positions and generate the indexes from there. So in other words, if I say draw a square around here, this innermost square, I have, and I'll just try and keep it there, I have four odd numbers and they all differ by two. So 3579. And if I draw another square here, next one out, I have four more numbers on the diagonal and they go 13, 17, 21, 25. And so as I go out each square, each concentric square, and this is a spiral matrix, so it has concentric squares. I am going to be adding four more odd numbers, but not every odd number because there are odd numbers that aren't on the diagonal, like 11 or 27 or 29. They're on the diagonal. I just want the ones that are on the diagonal. But if I could find those odd numbers on the diagonal, and if I could match them up with their indexes, I could find every other index just by working my way back from the nearest corner, and that's how I chose to solve it. So the first step is to find out if I give a value, what square am I working in? Because I don't want to be messing around in this square if I really want to be out in this square. So the way I do that is I had a verb that I called seed. And what seed did, if I give seed 29, because we're working with 29 as our example, seed says, I am going to give you back three. You're out in the third square here. And in fact, 29 is out in the third square. So it looks like it's working that way. How does seed work? I'm going to really blow through this because there's so much to Jay. If you really want to learn the language, you sit down and you start to play with this stuff. It's very interactive, which makes it a little easier to learn. But more importantly, you have to play with it. It's not something you can just jump in and do right away. It's starting to change the way you think about things. Anyway, having said that, what I've got here is a J verb. In fact, it's a composition of J verbs. So the first one is here going to be square root. So I'm taking 29, taking the square root of 29. And it's going to be a little bigger than five. I'm rounding it up with this verb. Round it up. I'm going to go to six. Perfect. I'm going to use this verb that's going to take six and half it. So I'm going to end up with three. And then this last one, I'm going to round down. Now for three, rounding down still gives me three. But if I'd had a different number coming in, it might have been 3.5, in which case I would round down to three. So seed 29 gives me three, just as you'd expect. And that's how it does it. Next up, I wanted to get all the diagonals. So I'm going to feed diagonal three from seed. That's where I got the three from is from seed. And I'm going to then generate all the values of the diagonals in the three cubes, including the one in the center. And there you go. It's got 1, 3, 5, 7, 9, skips 11. And then it goes to 13. And it starts jumping up by 4. 13, 17, 21, 25 does that. Then it jumps up to 31 and increases by six each time. So diagonal has been able to skip the entries that aren't on the diagonals. And it, or diagonal has been able to skip the entries that aren't on the diagonals and just give me only diagonal entries. So that's my way of making sure that I just get the diagonals. And now I will have the values relative to those diagonals. So whatever number I can pick up here, I can figure out where it is relative to a value. And that makes it a lot quicker for me to do my calculations. Now what I'm going to do my calculations based on are the indices. So to do that, I did this verb. And again, it's going to use the value from seed. And it's going to give the indices for these values. And the j in the center here is because I've represented as a complex number. So when I have the index 1, 1, I'm saying I'm going to represent it as a single number going 1j1. And say for when I have the value 2, 2, or the index 2, 2, I'm going to call it 2j2. That allows me to work with these just like they're single numbers, but retain the different values that they have within their indexes. So how did I do end? Did it like this? I take first 3, and I do integer. And 3 integer will give me 0, 1, 2. That's great. Got 0, 1, 2. And then when I get to here, I'm going to change it from 0, 1, 2 to 1, 2, 3. So 1, 2, 3. And then I'm going to multiply 1, 2, and 3 by this number here. But this little 1, 0 means 1 means I'm going to take this as a whole list. And 0 means I'm going to take this an atom at a time. So I'm multiplying this complex number by 1, 2, 3. Then I'm going to string it out in one long line, just like I've done here. So here you see all the ones that were multiplied by 1. And then all the ones were multiplied by 2. And then all the ones were multiplied by 3. And then I'm going to tack on at the front 0, 0j0. And so those become my indices. Now we look at spiral. And spiral is going to use these things that we already saw. It's going to generate the seed, the ring that I want to work with. It's going to take diagonal of the ring, and it's going to generate all the values that are on that. And then it's going to, with this, it's going to be the largest diagonal index for the value above the y value. So if I give seed as 29, I'm going to look for 31 is my largest value here. So I can actually check that. Oh, sorry, that's the index. Largest value is Lv. 31 is the largest index. I'm going to look in here. And in here is where I'm going to be generating the indices. And then I'm going to select the one that will give me the largest index, which will be 33. And so if I go here, 1, 2, 3, 1, 2, 3, I'm at 31. And the largest value was 31. So even though I'm sending in 29, now I know I'm looking at 29 relative to this position here. And this position here, I have the value and I have the index. And so then it's just a matter of calculating back to what the value of 29 is. But there's a few more lines in that. Got the largest value. We talked about that. Here, this is my exit clause. So if I happen to choose a value that was already on an index, if y29 was an element of the diagonal, because I've signed D to the diagonal, then I just return that to the index, because I've already found it. And so that's just the short. I don't have to go through all the other stuff. Now, if it's not there, not on the diagonal, this is where this comes in. So I look at lid, which is the largest index. And I'm going to calculate, based on the largest index, which one this is a verb and this is a verb and the backticks separate the verbs here. So that's a verb between those two backticks. Those little backticks separate the verbs. And this is called agenda. And it's a conjunction which says, if you give me a number on my right side, I will apply the number of that verb to this argument. So I send this argument into this to generate a number between zero and three, because J is zero indexed and then it will choose based on whether it's zero, one, two, or three, it'll choose whether it does this verb, this verb, this verb, or this verb. And the reason I do that is, depending on where you line up here, you would want to be decrementing your Y value, your second index. And if you were here relative to the next largest one, you would want to be decrementing your X value, your first index, and so on. So if I'm here, I'm going to be decrementing my first value. So I'll start from the value of 31, 33, I'm going to go back to, and I end up with 13, because I'm just taking the two off it. And I would go, well, let's see what I would end up doing here. I'll run this first and find out which verb I'm actually going to use. So I'm actually going to use three. So this is the one that I'm going to use here. And this verb here says, take the right argument, which is lid, and we know lid is 33. And this here is going to be 31. And Y, what we were giving is Y was 29. So when I do that, I'm going to say 29 minus 31 is minus two times one zero is minus two zero plus lid. Well, lid is 33 plus minus two zero should be one three. There you go. So that's how you do it in J. At least that's how I chose to do it in J. Now actually, while I was doing all this, I came up with a faster way to do it, but I maybe do a video about that tomorrow just to show you some of the things when I was putting this all together, I came up with a few ideas. But not that I'm expecting anybody to understand J based on this, but it has a pattern and it is consistent. And as soon as you have something that's consistent and has a pattern, if you learn those patterns, you can start to apply what you've learned to solving problems. And I thought this was kind of neat because I don't have to create all the indexes of every space. I just have to create the indexes of the diagonals. Okay, hope that helps.